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

Marc Durdin picture Marc Durdin · Nov 30, 2009 · Viewed 10.7k times · Source

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.

Answer

Marc Durdin picture Marc Durdin · Apr 30, 2013

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.