windows 如何在 Win x64 中的 32 位和 64 位应用程序之间共享 HWND?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1822667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I share HWND between 32 and 64 bit applications in Win x64?
提问by Marc Durdin
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?
MSDN 告诉我,在进程间通信(MSDN) 中,Windows 句柄 (HWND) 可以在 32 位和 64 位应用程序之间共享。但是,在 Win32 中,HWND 是 32 位,而在 64 位 Windows 中它是 64 位。那么如何共享句柄呢?
I guess the same question applies to handles to named objects such as mutexes, semaphores and file handles.
我猜同样的问题适用于命名对象的句柄,例如互斥锁、信号量和文件句柄。
采纳答案by Tim Sylvester
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.
它们可以共享的事实是否意味着在 Win64 进程中仅使用低 32 位?Windows 句柄是索引而不是指针,至少据我所知,所以除非 MS 想要允许超过 2^32 个窗口/文件/互斥锁/等。处理没有理由void*
在 Win64 上使用 a 的高 32 位。
回答by Marc Durdin
As Daniel Rose points out above, the MSDN documentationnow states:
正如 Daniel Rose 上面指出的,MSDN 文档现在指出:
... it is safe to truncate the handle (when passing it from 64-bit to 32-bit) or sign-extend the handle (when passing it from 32-bit to 64-bit).
...截断句柄(从 64 位传递到 32 位时)或符号扩展句柄(从 32 位传递到 64 位时)是安全的。
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.
鉴于 WOW64 开发人员告诉我零扩展是正确的方法,这里似乎仍然有些混乱。如果您正在编写从 32 位模块获取句柄的 64 位模块,最安全的方法可能是仅比较句柄的低 32 位(即截断)。否则,您可能会发现符号扩展与零扩展差异。
回答by Marc Durdin
I just received an email from a Microsoft WOW64 developer who confirms:
我刚刚收到一封来自 Microsoft WOW64 开发人员的电子邮件,他确认:
Handles are 32bit and can be safely truncated/zero extended. It is true for both kernel object handles and USER/GDI handles.
句柄是 32 位的,可以安全地截断/零扩展。对于内核对象句柄和 USER/GDI 句柄都是如此。
回答by Michael
Have a look in Microsoft Interface Definition Language (MIDL) Porting Guide, page 12 (http://msdn.microsoft.com/en-us/library/ms810720.aspx)
查看 Microsoft 接口定义语言 (MIDL) 移植指南,第 12 页 (http://msdn.microsoft.com/en-us/library/ms810720.aspx)
Here have a look ar USER and GDI handles are sign extended 32b values
这里看看 USER 和 GDI 句柄是符号扩展的 32b 值
回答by Mordachai
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.
我认为您总体上保持谨慎是正确的。但是,MSDN 声称它们可以共享是对我们程序员的契约。他们不能在不破坏大量软件的情况下说“今天分享”然后明天“不再分享”。
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.
同样,为了让 x64 和 32 位软件在给定的机器上同时运行,并且每个人都能相处融洽,HWND(和许多 HANDLE)必须继续是 32 位并兼容。
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".
我想我的意思是我认为这是一个非常安全的赌注,至少在 Windows 7 的生命周期中,并且可能是 Windows“下一个”。