Get Application's Window Handles

user361526 picture user361526 · May 4, 2009 · Viewed 86.1k times · Source

I'm building an app that given another app mainWindowhandle it collects information about the window state. I have no problem collecting information about child windows, but I can not access the other open windows of an application or even the menus. Is there any way of getting all window handles of an application?

Answer

Tim Robinson picture Tim Robinson · May 4, 2009

You could do what Process.MainWindowHandle appears to do: use P/Invoke to call the EnumWindows function, which invokes a callback method for every top-level window in the system.

In your callback, call GetWindowThreadProcessId, and compare the window's process id with Process.Id; if the process ids match, add the window handle to a list.