windowswinapinullhandle

Is there a difference if I added the window handle or NULL value


I have a little problem. I will show you an example firstly, then I will tell you what the problem.

Example:

while(GetMessage(&msg, NULL, 0, 0)){
   TranslateMessage(&msg);
   DispatchMessage(&msg);
}

That example work correctly, but if the parameter 2 in "GetMessage" function changed to a handle name of window as the following:

Example2: after changed

while(GetMessage(&msg, Hwnd, 0, 0)){
   TranslateMessage(&msg);
   DispatchMessage(&msg);
}

Will occurs a little problem when exit the program. still the program works in processes list. And need to select it then click on end process button to terminate the program

And now, Is there a difference between add (NULL or window handle)?


Solution

  • The problem you encounter in your second example is that after your window is destroyed, the window handle you supply to GetMessage() is no longer valid. Every subsequent call returns an error to notify you of that (with GetLastError() giving ERROR_INVALID_WINDOW_HANDLE), but the code doesn’t handle this case and so ends up in a busy loop forever.

    This is why the MSDN Library page for GetMessage() advises against using while (GetMessage(...)).