How can I share HWND between 32 and 64 bit applications in Win x64?
Asked Answered
M

5

24

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.

Mahlstick answered 30/11, 2009 at 21:56 Comment(0)
D
9

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.

Disproportionation answered 30/11, 2009 at 22:8 Comment(6)
I see the implication but there appears to be no specific documentation in MSDN stating that only the lower 32 bits are used and will ever be used and therefore it is safe to do this. An awareness of the current underlying implementation of window handles is not a guarantee that this won't change in future versions of Windows, given that this is not documented.Mahlstick
If there's one thing MS is good at, it's maintaining backward-compatibility. I agree that it would be nice to find some explicit statement on the matter, but it looks like an implicit statement is all you're going to get. Perhaps you should ask in one of the forums like microsoft.public.windows.64bit.general or microsoft.public.windows.app_compatibility (See aumha.org/nntp.php) Some MS developers must know for sure...Disproportionation
@Marc: The "specific documentation in MSDN" is the very statement you linked to. It doesn't really say that only the lower 32 bits are used, and in fact I'd expect Win64 to set at least one high bit - just to discover handles that have been truncated - but the lower 32 bits should be sufficient to uniquely identify a window.Photochemistry
@MSalters, if MS did set a high bit in Win64 then HWND comparison would be broken for handles passed from Win32. Your statement is a clear example of why we need to have clarity in these scenarios, not just assumptions and implicit guesses that truncation is therefore permissible. In other words, MSDN should say something like: "In Win64 a HWND uses only the lower 32 bits and the upper 32 bits are always zero. Therefore, to pass a HWND to Win32, truncate it to a DWORD32." That is explicit, specific documentation.Mahlstick
@Marc: I disagree. Truncation is obvious, and in fact the proper way to compare a HWND obtained from the Win32 API with one obtained from the Win64 API. This truncation should be done independenlty which side of the code (x86/x64) is actually executing the comparison.Photochemistry
@Photochemistry MSDN now states truncation/sign-extension explicitly as the proper way: msdn.microsoft.com/en-us/library/aa384203.aspxDoughman
M
22

As Daniel Rose points out above, the MSDN documentation now states:

... 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).

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.

Mahlstick answered 29/4, 2013 at 23:27 Comment(0)
M
14

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.

Mahlstick answered 1/12, 2009 at 23:57 Comment(0)
D
9

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.

Disproportionation answered 30/11, 2009 at 22:8 Comment(6)
I see the implication but there appears to be no specific documentation in MSDN stating that only the lower 32 bits are used and will ever be used and therefore it is safe to do this. An awareness of the current underlying implementation of window handles is not a guarantee that this won't change in future versions of Windows, given that this is not documented.Mahlstick
If there's one thing MS is good at, it's maintaining backward-compatibility. I agree that it would be nice to find some explicit statement on the matter, but it looks like an implicit statement is all you're going to get. Perhaps you should ask in one of the forums like microsoft.public.windows.64bit.general or microsoft.public.windows.app_compatibility (See aumha.org/nntp.php) Some MS developers must know for sure...Disproportionation
@Marc: The "specific documentation in MSDN" is the very statement you linked to. It doesn't really say that only the lower 32 bits are used, and in fact I'd expect Win64 to set at least one high bit - just to discover handles that have been truncated - but the lower 32 bits should be sufficient to uniquely identify a window.Photochemistry
@MSalters, if MS did set a high bit in Win64 then HWND comparison would be broken for handles passed from Win32. Your statement is a clear example of why we need to have clarity in these scenarios, not just assumptions and implicit guesses that truncation is therefore permissible. In other words, MSDN should say something like: "In Win64 a HWND uses only the lower 32 bits and the upper 32 bits are always zero. Therefore, to pass a HWND to Win32, truncate it to a DWORD32." That is explicit, specific documentation.Mahlstick
@Marc: I disagree. Truncation is obvious, and in fact the proper way to compare a HWND obtained from the Win32 API with one obtained from the Win64 API. This truncation should be done independenlty which side of the code (x86/x64) is actually executing the comparison.Photochemistry
@Photochemistry MSDN now states truncation/sign-extension explicitly as the proper way: msdn.microsoft.com/en-us/library/aa384203.aspxDoughman
K
2

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

Kobe answered 26/11, 2010 at 15:43 Comment(0)
K
1

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".

Kermitkermy answered 30/11, 2009 at 22:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.