I was recently asked where an interop dll can be found, so that an app’s C# code can call into the Windows UIA API. Whenever I'm working on a new app which leverages the awesome power of UIA, I always generate the interop dll myself. (Or I could root around my machine looking for an interop dll that I've generated in the past.) I've always found it quick 'n' easy to generate the interop dll once I got familiar with the steps. I could update the build steps for my project in Visual Studio to have the dll generated as part of the project build, but in my case, I only ever need the interop dll to be generated once, so I don't tend to do that.
The interop dll contains the metadata required for the calls from C# into the unmanaged Windows UIA API. Once I've generated the interop dll, I can view the object types from within Visual Studio's Object Browser while I'm working on my C# code. I can also view the contents using the ILDASM tool if I wanted to, as shown below.
Figure 1: ILDASM tool showing the contents of my UIA interop dll.
Anyway, below describes how I generate and work with the dll. Hopefully these steps will work for you too.
Whenever I’m working on a project where I’m calling the Windows UIA API from C# code, I use an interop dll that I’ve generated myself using a tool call tlbimp.exe. This might not produce exactly the same interface as the one exposed by the interop dll referenced in some older MSDN blog posts, but the results will be similar, and I always find it useful to be able to generate my own interop dll whenever I want.
So first I find where the tlbimp.exe tool is on my computer, by doing cd “C:\Program Files (x86)\Microsoft SDKs” in a command window, and then dir /s tlbimp.exe. Depending on the versions of the Windows developer tools I have, I may have different versions of tlbimp.exe. Once I’ve found the most current version I have, I use it to generate a UIA interop dll. For example, I might run this:
"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\TlbImp.exe" C:\Windows\System32\UIAutomationCore.dll -out:interop.UIAutomationCore.dll
When I do that, I get lots of warnings, but I still get the interop.UIAutomationCore.dll generated that I need. I can then go to my C# project in Visual Studio, and add a reference to my interop dll in my project’s list of references.
Figure 2: A reference to my UIA interop dll in the project's list of references.
I can then use the VS Object Browser to see all the classes available through the interop dll.
Figure 3: Using the Visual Studio Object Browser to view the object types available through the interop dll.
I can add “using interop.UIAutomationCore;” to the top of my C# file and start creating and using objects through the interop dll. For example:
CUIAutomation8 automation = newCUIAutomation8();
IUIAutomationElement element = automation.GetFocusedElement();
string focusedElementName = element.CurrentName;