Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the handle of window in C#

Tags:

I have the following class declared:

public partial class MainWindow : Window 

And I need to get the actual handle of the window once the window has one. How can I do that and where should I put the query function.

What I tried so far was:

IntPtr hwnd = new WindowInteropHelper(this).Handle; 

But the handle I get back is 0, which might be because it was planted in OnInitialized - maybe the window is not ready yet at that stage. And, yes - it is connected via WPF, thank you for pointing it out!

Thanks

like image 514
Adi Avatar asked Feb 18 '09 19:02

Adi


People also ask

How do I find my window handles?

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.

What is window handle in C#?

Once the Selenium WebDriver instance is instantiated, a unique alphanumeric id is assigned to the window. This is called window handle and is used to identify browser windows.

What is a handle to a window?

A handle is a unique identifier for an object managed by Windows. It's like a pointer, but not a pointer in the sence that it's not an address that could be dereferenced by user code to gain access to some data.

How can I get Hwnd from process id?

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


1 Answers

In the OnInitialized method the handle has not yet been created. But you are on the right track. If you put your call in the Loaded event the handle will have been created and it should return the correct handle.

like image 104
Stephen Martin Avatar answered Sep 21 '22 17:09

Stephen Martin