TypeLib2AHK - Convert COM Type libraries to AHK code

Post your working scripts, libraries and tools
Elgin
Posts: 99
Joined: 30 Sep 2013, 09:19

TypeLib2AHK - Convert COM Type libraries to AHK code

19 Aug 2017, 10:09

TypeLib2AHK

TypeLib2AHK is a free, open source tool to convert information about COM interfaces stored in type libraries to code usable with AutoHotkey 1.1 and AutoHotkey v2 (https://autohotkey.com/).

The aim is to make it more comfortable and seamless to work with COM from AutoHotkey by providing class wrappers for structures and non-dispatch interfaces which are not natively supported by AutoHotkey and also the named constants associated with the interfaces.

Download


How to use

TypeLib2AHK.ahk can either be run directly as a standalone application or be used in an AutoHotkey script to retrieve information about specific COM objects (see examples at the beginning of the code in TypeLib2AHK.ahk). It can be used with AutoHotkey 32bit and 64bit Unicode releases. AutoHotkey ANSI releases are not supported.

TypeLib2AHK.ahk currently only runs under AutoHotkey 1.1 but can create code for AutoHotkey 1.1 and AutoHotkey v2.

The standalone application offers the user a list of type libraries stored on the computer and retrieved from applications currently in memory. Type library information can also be loaded from DLL files.

Type libraries can be viewed or directly converted. The viewing functionality is fairly basic; to get more complete information you can use Oleview.exe which is part of the Windows SDK, which can be downloaded for free from Microsoft.


How to use the created code


The resulting code is structured as follows (all examples refer to the UIAutomationClient type library):

  • Header: Information about the type library.
  • CoClasses: The code in this section allows to instantiate the underlying COM interfaces. Example: UIA:=CUIAutomation()
  • Alias type definitions: For information only as type definitions are usually not relevant in AHK.
  • Constants (enumerations and modules): They can be used directly (Example: myTreeScope:=UIAutomationClientTLConst.TreeScope_Ancestors) or the wrapper class can be instantiated as needed. Reverse lookup of a constant name is also possible (Example ValueName:=UIAutomationClientTLConst.UIA_ControlTypeIds(Value))
  • Structures (records and unions): The structures are wrapped as classes for transparent use in AHK scripts (see Example code below). Wrapped structures can be used directly as parameters in function calls or wrapped interfaces.
  • Interfaces: The interfaces are wrapped as classes for transparent use in AHK scripts (see Example code below).
  • Dispatch interfaces: Dispatch interfaces are handled natively by AHK. Their contents are included for information only.
The code may not contain all the above sections, depending on what is defined in the respective type library.

IMPORTANT:
  • Before using the created code carefully read the definitions and make sure that it doesn't redefine any Autohotkey functions. As an example: mscorlib.dll overrides Object().
  • Many basic structures are defined in several type libraries. You may need to edit the created code to avoid doubles.
  • The converted AutoHotkey code will most likely be different for 32bit and 64bit (most notably: differences in structure offsets and variant handling in DLL-calls), so be sure to use the correct version and take care when manually merging the code for different bit versions.
Occasionally the type libraries make reference to interfaces or structures which are not included in the library. These can usually be found in other type libraries. Many basic structures and interfaces are defined in the type library "mscorlib.dll".


Known issues


  • some memory leaks

If you find errors or think something could be improved:
Please inform me about any errors in the created code that you find. I have (obviously) only tested this with a small number of type libraries and a huge number of things could go wrong.


Changes


  • v 0.95 added output of AHK v2 compatible files

Related

ImportTypeLib by maul.esel (https://github.com/maul-esel/ImportTypeLib) wraps type libraries directly at run-time. It requires a slightly more complex syntax in use and seems to have issues in 64bit.


Example code
Example code
Last edited by Elgin on 01 Sep 2017, 04:34, edited 1 time in total.
qwerty12
Posts: 449
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

19 Aug 2017, 10:11

Thank you!

(I've had problems with the IDispatch interface of one of the network interfaces in the past. FreePascal was the only language I tried that did what I wanted properly, and I noticed that used the TypeLib to do so.)
burque505
Posts: 141
Joined: 22 Jan 2017, 19:37

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

19 Aug 2017, 17:50

Elgin,
Do you have a link to UIAutomationClient_1_0_64bit.ahk?
I just Googled it and nothing shows up.
Many thanks!
burque505
Elgin
Posts: 99
Joined: 30 Sep 2013, 09:19

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

19 Aug 2017, 19:12

burque505 wrote:Elgin,
Do you have a link to UIAutomationClient_1_0_64bit.ahk?
I just Googled it and nothing shows up.
Many thanks!
burque505


No need for a link. That's the job of TypeLib2AHK. It creates that file (and many more libraries) for you:

Download+unzip the TypeLib2AHK repository linked in my post, run TypeLib2AHK.ahk, select UIAutomationClient from the list and press Convert.
burque505
Posts: 141
Joined: 22 Jan 2017, 19:37

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

20 Aug 2017, 13:27

Elgin,
That is absolutely awesome. Hats off. Thank you!!
cpriest
Posts: 20
Joined: 17 Sep 2017, 08:06

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

23 Sep 2017, 16:54

Elgin, I agree, this is absolutely awesome. I had been struggling with ImportTypeLib and on a whim decided to search the forums for more recent information and found this. This is really great stuff.

I already posted this same exact question here https://autohotkey.com/boards/viewtopic.php?f=5&t=37142&hilit=ImportTypeLib+event+handler and got a great (and working answer), but that is using uia.ahk which I won't get into more except that I'd prefer to use your generated code.

Trouble is I need to implement IUIAutomationEventHandler and based on the class that it generated (and what I gleaned from the help of my other post), what is here would not be sufficient (as I understand the situation). Here is what your code produced (v1.1 64 bit):

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



Transgarbling what I think I know about this, it's expecting a pointer which looks like it expects it to be a vtable, it also implements a HandleAutomationEvent() which seems it would call the 4th function in the vtable, though I'm not sure what would call that function (HandleAutomationEvent()).

In any case, it would seem this is an Interface class and not an implementation of the interface.

  • Is there a way to get TypeLib2AHK to build an implementation for this type of interface?
  • Is there a library out there for building these kinds of implementations?
  • Would you have an idea how to manually build an implementation for this IUIAutomationEventHandler with using your code generation tool?
Thanks!

P.S. I'd be happy to contribute to your project to implement this automated building of an implementation class for these types of things if the type library gives any indication that an implementation would be needed for the interface to be of use (since in this case, I don't think windows would ever give back a pointer to an implementation).
Elgin
Posts: 99
Joined: 30 Sep 2013, 09:19

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

24 Sep 2017, 10:07

Hi cpriest,

first off I must say that I have never tried around with UIA event handlers or implemented my own COM objects in AHK, so my knowledge on this is very limited.

The code you quoted is indeed not intended to implement the interface. It merely gives you a wrapper to access an existing object having that interface.

In theory it should be no problem to have these implementations automatically written. After all, at it's core TypeLib2AHK merely patches pre-written strings and formatted data from the type libraries together into a file. The same procedure should work for basic implementation skeletons.

The more interesting point is to distinguish when an interface should be implemented and when it should be wrapped as the two seem mutually exclusive (at least I don't see a way to wrap existing COM objects and implement them at the same time). I've looked at the raw data from the UIA type library and noticed that all the event handlers have a TYPEFLAG_FOLEAUTOMATION flag set which no other interface in the library has. The MSDN documentation on this flag is not all that enlightening ("The types used in the interface are fully compatible with Automation, including VTBL binding support. Setting dual on an interface sets this flag in addition to TYPEFLAG_FDUAL. Not allowed on dispinterfaces.") imho, so I'm not really sure if this is a good indicator that an implementation is needed for every type library out there. But it sounds good enough to try...

If you could provide me with a working implementation code (i.e. what you'd like to see written in the generated code file instead of what's there now) and some sample code to try it out, I can try to patch that into my code. Fair warning: I'm rather busy at the moment, so it might take a while.
cpriest
Posts: 20
Joined: 17 Sep 2017, 08:06

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

24 Sep 2017, 17:19

Sure, I'll put something together and post it here when I get it working properly.
cpriest
Posts: 20
Joined: 17 Sep 2017, 08:06

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

01 Oct 2017, 15:25

Elgin wrote:If you could provide me with a working implementation code (i.e. what you'd like to see written in the generated code file instead of what's there now) and some sample code to try it out, I can try to patch that into my code. Fair warning: I'm rather busy at the moment, so it might take a while.


Hey Elgin, I have a fully functional example here now. This isn't precisely how I would have it output by TypeLib2AHK since I figured you might not be able to get to this right away. Probably your output would be less DRY than this, but if you smooshed these classes together and Eliminated some of the "don't create a new vTable for each new handler code I have in there, you'd be down to the basics.

LMK if you have any questions about it, the one include at the top is the output from TypeLib2AHK for the UIAutomation Client Type Library

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by cpriest on 02 Oct 2017, 20:54, edited 1 time in total.
Elgin
Posts: 99
Joined: 30 Sep 2013, 09:19

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

02 Oct 2017, 01:24

Thanks. I'll look into it.
cpriest
Posts: 20
Joined: 17 Sep 2017, 08:06

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

02 Oct 2017, 16:54

You know, after writing this and trying to solve some of the problems I had originally, which eventually became the above, I have some other ideas I'm going to try out. I'll leave the above posted for reference, but I'll post another time in the future once I've refactored it some more.

In essence, it will be the same, but cleaner.
Elgin
Posts: 99
Joined: 30 Sep 2013, 09:19

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

02 Oct 2017, 17:47

I gave your code a quick test and it only seems to run in 64bit but not in 32. Any idea what might cause this? I'd like to support both if possible.
User avatar
jeeswg
Posts: 3000
Joined: 19 Dec 2016, 01:58
Location: UK

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

02 Oct 2017, 19:11

Re. x64/x32, the one thing I noticed was this.
HeapAlloc: Ptr,UInt,>UPtr< (UPtr not UInt).
cpriest
Posts: 20
Joined: 17 Sep 2017, 08:06

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

02 Oct 2017, 20:30

Hmm yeah, I hadn't tried it in 32 bit, did that fix it?

Edit:
Ok, yeah I see how that should be the case based on it being SIZE_T -> ULONG_PTR -> unsigned int64 (64 bit) or just a regular long (32 bit) in 32 bit.
User avatar
jeeswg
Posts: 3000
Joined: 19 Dec 2016, 01:58
Location: UK

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

02 Oct 2017, 21:47

I had a look through the code, I haven't tested.
Elgin
Posts: 99
Joined: 30 Sep 2013, 09:19

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

03 Oct 2017, 08:07

I've tested it; still doesn't work in 32bit. I've logged the function calls and found that the call to AddAutomationEventHandler doesn't complete in 32 bit.

Here are the logs:
64bit startup:
Spoiler


A number of interfaces which seem to be mostly about marshaling get queried and most calls fail obviously.

64bit window open event:
Spoiler


32bit:
Spoiler


After the query for INoMarshal there are simply no more queries and the call to AddAutomationEventHandler never returns.

To try around I added INoMarshal to the supported interfaces. The call to AddAutomationEventHandler then returns but no events are received, so this doesn't solve the problem either:
Spoiler


When INoMarshal is added in 64bit, the call to AddAutomationEventHandler fails with 0x80004021 (CO_E_NOT_SUPPORTED) btw.
cpriest
Posts: 20
Joined: 17 Sep 2017, 08:06

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

03 Oct 2017, 19:06

I'm not sure what's going on exactly with the 32-bit stuff, but I'll test it out and see what I can find out.

I did run into that issue before I had posted my solution, drove me nuts and I had discovered that _QueryInterface() was incorrectly returning a decimal rather than hex, that is this line:

Code: [Select all] [Download] GeSHi © Codebox Plus

		return 0x80004002 ; E_NOINTERFACE      <--- Correct

return 80004002 ; E_NOINTERFACE <--- Incorrect


That was precisely the cause of that exact same effect I was getting. Perhaps the 0x80004002 is being returned as unsigned or signed incorrectly in 32-bit?

On another note, I've got an update to the code. What I was thinking didn't work out but made me realize the parameter juggling I was doing wasn't necessary so this is cleaner. Posting again in case you want to diff them.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Elgin
Posts: 99
Joined: 30 Sep 2013, 09:19

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

03 Oct 2017, 20:53

The 0x80004002 does not seem to be the issue. Returning -2147467262, 0x80004002 or 0xFFFFFFFF80004002 all produces identical results. But when any other value is returned, the final AddRef does not get called anymore which I assume means that QueryInterface went wrong. So it looks like things are going right from our side.
Strange.

Btw: did you come across any example of this successfully being done in 32bit in another non-.net programming language? Maybe the fault is not even with us...

Another thing:
Some interfaces contain properties in addition to functions e. g. IRawElementProviderSimple. How should those be implemented? Like other functions (after all type libraries have separate vtable entries for property get and put) or in a different way?
cpriest
Posts: 20
Joined: 17 Sep 2017, 08:06

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

03 Oct 2017, 21:00

Hmm, I don't know enough about com with that, do you know of an article that discusses the memory structure of a com pointer?
Elgin
Posts: 99
Joined: 30 Sep 2013, 09:19

Re: TypeLib2AHK - Convert COM Type libraries to AHK code

06 Oct 2017, 09:17

Hi,

I've uploaded the changes so you can give them a try:
https://github.com/Elgin1/TypeLib2AHK/tree/Test

Was there any particular reason for shifting the function parameters and inserting pInterface? I noticed it creates big problems with byref parameters and removed it. Seems to have no ill effect...

For some reason your original code and my adaptation of your example only seems to receive a single event and then nothing more. Do you have any idea why that could be?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 12 guests