tags:

views:

454

answers:

3

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.

+2  A: 

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.

Tim Sylvester
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.
Marc
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 http://www.aumha.org/nntp.php) Some MS developers must know for sure...
Tim Sylvester
@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.
MSalters
@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.
Marc
@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.
MSalters
A: 

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

Mordachai
+1  A: 

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.

Marc