Bill Agee's blog

🤔 Reflections on test infrastructure, with a twist of user empathy.

Pure Ruby example of calling functions from the MS IUIAutomation COM interface

Placeholder screenshot

A while back I was interested in writing some Ruby code that used functions from the Windows IUIAutomation COM interface.

But since IUIAutomation is a custom COM interface that doesn't implement IDispatch, Ruby's Win32OLE module won't work with it.

And I wanted to avoid using a C extension that wraps UI Automation - see RAutomation for an example of a nice library that takes the C extension approach.

Without using a C extension, the only way I've found to use IUIAutomation in Ruby is to use Windows::COM - which is included in the awesome windows-pr project.

Then, one must pore over the C header file containing the function prototypes you want to use (UIAutomationClient.h in this case) and port each prototype to Ruby one at a time (!).

Note you have to install the the Windows 7 SDK to get a copy of that header file, but you don't need the SDK or .h to simply run the Ruby script shown below.

(For what it's worth, my copy of the header file is at \Program Files\Microsoft SDKs\Windows\v7.1\Include\UIAutomationClient.h)

This is all quite a far cry from how easy the same task is in the CPython world thanks to comtypes. But I digress - doing the same thing in Python deserves its own post.

Below is a Ruby script that obtains and prints the name of the root (Desktop) UI automation element on a Windows box - the string should always be "Desktop".

Doing so shows how to use IUIAutomation::GetRootElement() and IUIAutomationElement::get_CurrentName().

Apologies for the convoluted hacks on display - stuff like having to hardcode the number of each function in the IUIAutomationVtbl struct will hopefully have a better solution someday!

NOTE: Before running this you must:

Comments