I'm programming a console application in C++ in Visual Studio 2022 and I would like to get the IUIAutomationElement of the "main form" (the big, outer form) of a specific application so that I can later get its children. I see two approaches:
- Using the Process ID or Process Name. I have working source code that can get the Process ID from the Process Name and, of course, the Process Name (ends with .exe) is known.
- In a "find all applications"-manner and then filter the desired application by comparing a substring (not the full string because this can vary!) in the form's title.
Please note that I don't want to use any location (e.g. the mouse location) to locate the IUIAutomationElement. Can someone please help me?
With help from @SimonMourier and some googling, I finally solved it. First, I need to get the first dwThreadId from my dwProcessId and I do that using the function GetFirstThreadId:
Then it's just a matter of calling EnumThreadWindows and ElementFromHandle: