如何在 Win x64 中的 32 位和 64 位应用程序之间共享 HWND?
MSDN 告诉我,Windows 句柄 (HWND) 可以在 32 位和 64 位应用程序之间共享,位于 进程间通信 (MSDN)。但是,在 Win32 中,HWND 是 32 位,而在 64 位 Windows 中,它是 64 位。那么如何共享句柄呢?
我想同样的问题也适用于命名对象的句柄,例如互斥体、信号量和文件句柄。
MSDN tells me that handles to windows (HWND) can be shared between 32- and 64-bit applications, in Interprocess Communication (MSDN). However, in Win32 a HWND is 32 bits, whereas in 64 bit Windows it is 64 bits. So how can the handles be shared?
I guess the same question applies to handles to named objects such as mutexes, semaphores and file handles.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如 Daniel Rose 上面指出的,MSDN 文档现在指出:
鉴于 WOW64 开发人员告诉我零扩展是正确的方法,这里似乎仍然存在一些混乱。如果您正在编写一个从 32 位模块获取句柄的 64 位模块,最安全的选择可能是仅比较句柄的低 32 位(即截断)。否则,您可能会发现符号扩展与零扩展的差异。
As Daniel Rose points out above, the MSDN documentation now states:
There still seems to be some confusion here, given that I was told zero extension is the correct way by a WOW64 dev. If you are writing a 64 bit module that gets handles from 32 bit modules, the safest bet might be to compare only the lower 32 bits of the handle (i.e. truncate). Otherwise, you may be caught out on a sign-extension vs zero-extension difference.
我刚刚收到一封来自 Microsoft WOW64 开发人员的电子邮件,他确认:
句柄是 32 位的,可以安全地截断/零扩展。对于内核对象句柄和 USER/GDI 句柄都是如此。
I just received an email from a Microsoft WOW64 developer who confirms:
Handles are 32bit and can be safely truncated/zero extended. It is true for both kernel object handles and USER/GDI handles.
它们可以共享的事实难道不意味着在 Win64 进程中只使用低 32 位吗? Windows 句柄是索引而不是指针,至少据我所知,所以除非 MS 希望允许超过 2^32 的窗口/文件/互斥体/等。句柄没有理由在 Win64 上使用
void*
的高 32 位。Doesn't the fact that they can be shared imply that only the lower 32 bits are used in Win64 processes? Windows handles are indexes not pointers, at least as far as I can tell, so unless MS wanted to allow more than 2^32 window/file/mutex/etc. handles there's no reason to use the high 32 bits of a
void*
on Win64.查看 Microsoft 接口定义语言 (MIDL) 移植指南,第 12 页
(http://msdn.microsoft.com/en-us/library/ms810720.aspx)
这里看看 ar USER 和 GDI 句柄是符号扩展的 32b 值
Have a look in Microsoft Interface Definition Language (MIDL) Porting Guide, page 12
(http://msdn.microsoft.com/en-us/library/ms810720.aspx)
Here have a look ar USER and GDI handles are sign extended 32b values
我认为你总体上保持谨慎是正确的。然而,MSDN声称可以共享,这是对我们程序员的一种契约。他们不能很好地说“今天分享”,然后明天“不再分享”,否则就会破坏大量软件。
同样,为了让 x64 和 32 位软件在给定机器上同时运行,并且每个人都能相处融洽,HWND(和许多 HANDLE)必须继续是 32 位且兼容。
我想我想说的是,我认为这是一个非常安全的赌注,至少对于 Windows 7 以及可能的 Windows“下一个”的生命周期来说是这样。
I think you're right to be cautious in general. However, MSDN claiming that they can be shared is a contract to us programmers. They can't well say "share it today" and then "no longer" tomorrow, without breaking a great deal of software.
Similarly, for x64 and 32bit software to run concurrently on a given machine, and for everyone to get along, HWNDs (and many HANDLEs) must continue to be 32bit and compatible.
I guess what I'm saying is that I think this is a very safe bet, at least for the lifetime of Windows 7, and likely Windows "next".