Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Application's Window Handles

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?

like image 766
user361526 Avatar asked May 04 '09 16:05

user361526


People also ask

How do you get window handles?

Get the handle of the parent window using the command: String parentWindowHandle = driver. getWindowHandle(); Print the window handle of the parent window. Find the element on the web page using an ID which is an element locator.

How do I get a window handle in C++?

The proper way, in my humble opinion, is handle=FindWindowW(NULL, L"Calculator"); (using UNICODE) or handle=FindWindowA(NULL, "Calculator");(using ANSI). Even better, use instead FindWindow macro to avoid know what character set is used in the project, so make your code "agnostic" to character set.

How do I find my windows handle name?

The Win32 API provides no direct method for obtaining the window handle associated with a console application. However, you can obtain the window handle by calling FindWindow() . This function retrieves a window handle based on a class name or window name. Call GetConsoleTitle() to determine the current console title.

How do I get Hwnd from process handle?

You can use EnumWindows and GetWindowThreadProcessId() functions as mentioned in this MSDN article.


2 Answers

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.

like image 99
Tim Robinson Avatar answered Oct 13 '22 14:10

Tim Robinson


First, you'll have to get the windowhandle of the application's mainwindow.

 [DllImport("user32.dll", SetLastError = true)]
 static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

 IntPtr hWnd = (IntPtr)FindWindow(windowName, null);

Then, you can use this handle to get all childwindows:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);

private List<IntPtr> GetChildWindows(IntPtr parent)  
{  
    List<IntPtr> result = new List<IntPtr>();  
    GCHandle listHandle = GCHandle.Alloc(result);  
    try  
    {  
         EnumWindowProc childProc = new EnumWindowProc(EnumWindow);  
         EnumChildWindows(parent, childProc, GCHandle.ToIntPtr(listHandle));  
    }  
    finally  
    {  
         if (listHandle.IsAllocated)  
               listHandle.Free();  
    }  
    return result;  
}   
like image 30
Mez Avatar answered Oct 13 '22 15:10

Mez