Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[AHK_L] ImportTypeLib - extended COM support!


  • Please log in to reply
22 replies to this topic
maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
This library is supposed to significantly ease COM programming in AutoHotkey.

Preview:
[color=red]UIAutomation := ImportTypeLib(A_WinDir "\System32\UIAutomationCore.dll")[/color]
automation := new UIAutomation.IUIAutomation(new UIAutomation.CUIAutomation())

MsgBox % "Retrieved condition: " automation.ControlViewCondition
automation.RemoveAllEventHandlers()

desktop := new UIAutomation.IUIAutomationElement(automation.GetRootElement())
MsgBox % "Desktop process PID: " desktop.CurrentProcessId
desktop.SetFocus()

MsgBox % "The desktop has " . (desktop.CurrentOrientation == UIAutomation.OrientationType.Horizontal ? "horizontal" : "vertical") . " orientation."
_____________________________________________________________________________________________________________________________________________________

Basics:
Since the support of memory addresses in DllCall, COM can be called from within AutoHotkey. This way is a little inconvenient, and so the COM stdlib was created. With AHK_L, builtin support for calling dispatch interfaces was added.
However, there is still a lot of COM that can't be called like that. This still needed to be invoked using DllCall - until now.
_____________________________________________________________________________________________________________________________________________________

Usage:
This library allows you to call COM methods the same way as you do for dispatch interfaces (such as Office, InternetExplorer and more). It also adds more convenience: you can also use enumerations and structures.

To use it, however, you need a COM type library. There are a lot of them registered with your system. You can view them using this script (included in the download). If you have the Windows SDK, there are even more libs which can be compiled from *.idl files. However, the end-user also requires this lib (ensure you are allowed to redistribute a lib if needed).

In your script, call ImportTypeLib() with either the file path, or, if it is registered within the system, the library GUID and the library version. The return value is an object representing the type library.

You can use it as shown in the code above to retrieve enum values, create instances of included classes (Attention: this returns a pointer which must be wrapped by creating an instance of an interface.), interfaces and structures (not yet supported). From created interface instances you can call methods and retrieve or set properties just as with a dispatch object.

There are also more methods in the typelib object, and even more will be added.

Note: error handling is done with exceptions. So whenever something fails, an exception will be thrown.
_____________________________________________________________________________________________________________________________________________________

Known limitations:
[*:306pysrr]Structures are not yet supported
    Edit 25/03/2012: Structures can be instantiated, members can be set. Passing structures to methods and more advanced usage is still missing.
[*:306pysrr]It is not supported to call AHK-builtin object methods on the created objects. Use the Obj*(obj, ...) form. However, avoid adding or removing fields.
    Edit 13/03/2012: Enumerations now support _NewEnum() and can therefore be used with a for-Loop.
    Edit 14/03/2012: The same now applies to modules.
    Edit 25/03/2012: ... and for structures.
    Edit 27/03/2012: Structures may also be cloned using struct._Clone() or struct.Clone(). Doing so results in an entirely new structure with new memory space, initialized with the values of the original.
                                    Subsequent changes to either the original or the clone do not affect the other one.

[*:306pysrr]There may be some debugging messages left :)
    Edit 13/03/2012: should be removed now.
[*:306pysrr]The entire thing will be made more stdlib-compatible.
    Edit 17/03/2012: Function prefixes (ITL_) and all-in-one file should accomplish that.
[*:306pysrr]The loaded type information might refuse to do some things.
    Edit 24/03/2012: For example, methods marked with the [restricted] attribute, such as IUnknown and IDispatch methods, cannot be called.
[*:306pysrr]Comments / documentation is still missing.
    Edit 25/03/2012: documentation has been started, first stuff is available under maul-esel.github.com/ImportTypeLib_____________________________________________________________________________________________________________________________________________________

Feedback required
This is still in early alpha stage, so please report anything you discover. Also make suggestions if you feel something could be improved. Of course anyone familiar with raw COM or OOP in AHK is welcome to fork and contribute on github or to submit fixes here.
_____________________________________________________________________________________________________________________________________________________

Download (for regular users)
Download Project (for developers)
As stated in the post title, this requires (latest) AHK_L. It will later be ported to AHK v2.

Regards
maul.esel
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
[reserved]
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

tinku99
  • Members
  • 560 posts
  • Last active: Feb 08 2015 12:54 AM
  • Joined: 03 Aug 2007
:shock:
Wow, thanks for the amazing effort.

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
You're welcome. :)

Btw, does anyone know what "modules" do in COM? I'm not quite sure how to handle those, therefore they're currently ignored. The same applies for unions (might get added later) and typedefs ("aliases" - I couldn't see any use for them).
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
See How To Make C DLL More Accessible to VB with a Type Library.

hughman
  • Members
  • 192 posts
  • Last active: Feb 14 2016 06:59 AM
  • Joined: 11 Feb 2007
why the type library can't be loaded in your example of ui animation?
my system: winxp sp3, ahk_u 32bit

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
I should have noted this: the example requires

Minimum supported client - Windows 7, Windows Vista with SP2 and Platform Update for Windows Vista, Windows XP with SP3 and Platform Update for Windows Vista

Are you missing that "Platform Update" :?: (what is that even?)

Edit: Does the file specified exist on your system?
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
@Lexikos: Thanks for that link. It seems to me modules are just containers for variables and functions? So I'll check out if ITypeInfo::Invoke() works for modules... And then I'll probably add support for them.
        Edit: Support for retrieving constants from modules was added, methods are still missing.

@All: the thing seems to have issues with 64bit (bug report #1), I'll check how I can fix it.
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
What do you think?
Should it be allowed for internal methods to be called on the created objects?
The problem with that is that e.g. interfaces could have methods with the same name. Currently, support for calling the builtin methods if a method is not found is disabled for consistency. You can use ObjClone(obj) or similar. Honestly, I don't really see any use for allowing obj.Clone() ("Clone" is just an example here).

About modules: of course they do not support Invoke() (there's no instance to pass, and it's stated that it is for interfaces only), but I think I found another way to call them.
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
An all-in-one download file is available now (see top post). Also it now works for me on 64bit (needs to be confirmed).
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

fincs
  • Moderators
  • 1662 posts
  • Last active:
  • Joined: 05 May 2007
Could you apply this diff to your repo? Not everyone has 30000x20000 monitors:
diff --git a/Viewer.ahk b/Viewer.ahk
index 880d445..ddaf500 100644
--- a/Viewer.ahk
+++ b/Viewer.ahk
@@ -11,23 +11,27 @@ libFile := ""
 Menu ViewMenu, Add, Open registry, openRegLibs
 Menu ViewMenu, Add, Open Folder, openFolderLibs
 Menu ViewMenu, Add, Open file, openFileLibs
+Menu ViewMenu, Add, Copy selected row, CopySelected
 
 Menu GuiMenu, Add, View, :ViewMenu
 
 Gui view: Menu, GuiMenu
 Gui view: +Resize
-Gui view: Add, Listview, x5 y5 w1200 h800, Name|Description|GUID/Path|Version
-Gui view: Add, Button, x5 y810 w100 h50 gCopySelected, Copy selected row
+Gui view: Add, Listview, vLVr x0 y0 w800 h600, Name|Description|GUID/Path|Version
 
 gosub UpdateLibs
 
-Gui Show, w1210 h870, Type Library Viewer
+Gui Show, w800 h600, Type Library Viewer
 return
 
 viewGuiClose:
 	ExitApp
 return
 
+viewGuiSize:
+GuiControl view:Move, LVr, w%A_GuiWidth% h%A_GuiHeight%
+return
+
 openFolderLibs:
 	FileSelectFolder libFile, C:\, 2, Select the folder holding the type libraries
 	if !ErrorLevel

Other than that, this looks amazing :D, I'll test it thoroughly when I have time.

sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
Works for me so far.

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
@fincs: done, thanks
Thanks sinkfaze.
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
I couldn't do a lot of changes the last days, have been busy with other things.

Also, I've struggled working on structure support. Please help me. Edit: [solved]
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit

maul.esel
  • Members
  • 790 posts
  • Last active: Jan 05 2013 09:26 PM
  • Joined: 28 Feb 2011
Me again :)
Any more feedback, please?

And another question:
Do you think arrays (not SAFEARRAYs) should be zero-based or one-based :?: In AHK, the one-based concept is more common, however, you'll always have to keep that in mind when translating other languages or using information from msdn...

I also noticed that it has an ugly bug (setting properties throws an error) which I could not yet fix (see #13). Need to check that out thoroughly :? [ Edit: Solved, thanks again, Lexikos :!: ]
Join the discussion on The future of AutoHotkey
Posted Image Visit me on github Posted Image
Win7 HP SP1 64bit | AHK_L U 64bit