Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Window Handle in .NET change it's value?

During the lifetime of a .NET process, does the handle of a System.Windows.Forms.Form, lets say the main form used in Application.Run(form) actually change it's value, i.e. if using the value of the handle in a different process, e.g. IntPtr handle = User32.FindWindow(null, "Name"), is there a case where that handle might be invalidated by the .NET runtime?

EDIT

I need to know the handles because I want to use SendMessage and WM_COPYDATA and the like for IPC.

like image 360
Sebastian Avatar asked Jul 21 '11 07:07

Sebastian


People also ask

How many handles can windows handle?

There is a theoretical limit of 65,536 GDI handles per session. However, the maximum number of GDI handles that can be opened per session is usually lower, since it is affected by available memory.

What is windows 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. Since the id is unique, it is used by the Selenium WebDriver to switch between different windows (or tabs).

Is a windows handle a pointer?

HANDLEs are defined as void pointers (void*). They are used as unique identifiers to each Windows object in our program such as a button, a window an icon, etc.

How do I get the handle of window in CPP?

However, you can obtain the window handle by calling FindWindow() . This function retrieves a window handle based on a class name or window name.


1 Answers

A window handle is guaranteed to be valid and not get reused for as long as the window lives. It's index like in nature, valid globally and generally behaves more like a global ID than like a kernel handle(which are only valid in one process and pointer like in nature). Once the window gets closed the window handle might get reused and now points to another window.

But what's not obvious is if the lifetime of the Form and the underlying windows window are the same. I vaguely remember that in Delphi's VCL(Which is the spiritual predecessor of Windows.Forms) certain property changes recreated the window in the background.

The existence of the Control.RecreatingHandle property seems like a strong indication that indeed the lifetime of the underlying window can be shorter than the lifetime of the .net control. Which could lead to the handle of a Form changing during its lifetime.

Control.RecreateHandle
The RecreateHandle method is called whenever parameters are needed for a new control, but using a call from UpdateStyles to CreateParams is insufficient. This method also calls DestroyHandle and CreateHandle and sets RecreatingHandle to true.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.recreatehandle.aspx

From the description of this method I conclude that the window handle can indeed change during the lifetime of the form.

like image 156
CodesInChaos Avatar answered Oct 11 '22 13:10

CodesInChaos