可以在.NET中的窗口句柄改变它的价值呢?它的、句柄、窗口、价值

2023-09-04 00:55:23 作者:霸道是我的个性

在一个.NET进程的生命周期,做的手柄 System.Windows.Forms.Form中,可以说在 Application.Run(形式)真正改变它的值,也就是说,如果使用在不同的进程句柄的值,例如 IntPtr的处理= User32.FindWindow(NULL,姓名),是在那里有这个句柄可能由.NET运行库而失效呢?

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?

修改

我需要知道的把手,因为我想用 SendMessage函数 WM_COPYDATA 之类的IPC。

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

推荐答案

一个窗口句柄保证是有效的,而不是得到重用,只要窗口生活。它的索引像性的,有效的全局和一般行为更像一个全局ID不是像一个内核手柄(这仅适用于一个进程和指针像性的)。一旦窗口被关闭的窗口句柄可能会被重用,现在指向另一扇窗。

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.

但不是很明显的是,如果在表格和基本的Windows 窗口的寿命是相同的。我依稀记得,在Delphi的VCL(这是Windows.Forms的的精神predecessor)某些属性更改再现了窗口的背景。

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.

的 Control.RecreatingHandle 属性似乎是一个强有力的迹象表明,的确是基础窗口的寿命可以比.NET控件的寿命短。这可能导致一个表格在其寿命期间改变的句柄。

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   所述RecreateHandle方法被调用时都需要一个新的控制参数,但使用的是呼叫从UpdateStyles到的CreateParams不足。这种方法也叫DestroyHandle和CreateHandle并设置RecreatingHandle为true。   的http://msdn.microsoft.com/en-us/library/system.windows.forms.control.recreatehandle.aspx

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.