0

I have some trouble understanding one particular thing. My computer is running 64-bit Windows, and so

std::cout << sizeof(HANDLE) << std::endl;
std::cout << sizeof(HWND) << std::endl;
std::cout << sizeof(int*) << std::endl;

all prints 8, i.e. 8 bytes (64 bits).

Now, in the Window Procedure

LRESULT __stdcall wndProc(HWND, UINT, WPARAM, LPARAM lParam)

the sizes (in bytes) of WPARAM and LPARAM are also 8. I recall reading in Petzold's book, however, that there are some messages for which a handle to a window is stored in either the LOWORD or the HIWORD of the LPARAM argument. For example,

HWND childHandle = (HWND)LOWORD(lParam);

How can this be? The HIWORD of lParam, is the top two bytes of a 32-bit integer? In order to store a handle in an LPARAM, would that require all eight bytes?

Thanks!

jensa
  • 2,792
  • 2
  • 21
  • 36
  • http://stackoverflow.com/questions/1822667/how-can-i-share-hwnd-between-32-and-64-bit-applications-in-win-x64 – Yury Schkatula Jan 26 '15 at 15:31
  • 1
    Ah thanks, this link has some good comments: https://msdn.microsoft.com/en-us/library/aa384203.aspx 64-bit versions of Windows use 32-bit handles for interoperability. When sharing a handle between 32-bit and 64-bit applications, only the lower 32 bits are significant, so 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). Handles that can be shared include handles to user objects such as windows (HWND), handles to GDI objects such as pens and brushes (HBRUSH and HPEN), and handles to named objects... – jensa Jan 26 '15 at 15:34

2 Answers2

1

A quick search leads to this MSDN page that states this approach was used in 16-bit applications and was changed after move to 32-bit architecture:

You extract the remaining two values in this way in the 16-bit framework:
HWND hWndCtrl = (HWND)LOWORD(lParam); //Control handle
int nCode = HIWORD(lParam);           //Notification code

You extract them this way in the 32-bit framework:
HWND hWndCtrl = (HWND)lParam;              //Control handle
int nCode = HIWORD(wParam);                //Notification code
dewaffled
  • 2,850
  • 2
  • 17
  • 30
0

Are you sure you aren't reading the sample wrong and thinking of a control ID? You're correct to have suspicions -- HWND is an obfuscated pointer, not a 16-bit value.

WM_COMMAND, for instance:

UINT wNotifyCode = HIWORD(wParam); 
UINT wID = LOWORD(wParam); 
HWND hwndCtl = (HWND)lParam;
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110