23

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.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
Marc Durdin
  • 1,675
  • 2
  • 20
  • 27

5 Answers5

21

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.

Marc Durdin
  • 1,675
  • 2
  • 20
  • 27
13

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 Durdin
  • 1,675
  • 2
  • 20
  • 27
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.

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
  • 8
    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 Durdin Nov 30 '09 at 22:14
  • 1
    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 Nov 30 '09 at 22:26
  • 1
    @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 Dec 01 '09 at 09:32
  • 2
    @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 Durdin Dec 01 '09 at 22:07
  • @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 Dec 02 '09 at 10:49
  • 7
    @MSalters MSDN now states truncation/sign-extension explicitly as the proper way: http://msdn.microsoft.com/en-us/library/aa384203.aspx – Daniel Rose Apr 18 '12 at 08:32
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

Michael
  • 21
  • 1
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".

Mordachai
  • 9,412
  • 6
  • 60
  • 112