Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostQuitMessage(WM_QUIT) vs PostQuitMessage(0)

Tags:

windows

winapi

What is the differences between

case WM_DESTROY:
    PostQuitMessage(WM_QUIT);
    break;

and

case WM_DESTROY:
    PostQuitMessage(0);
    break;

?

like image 689
zzz Avatar asked Apr 08 '26 18:04

zzz


2 Answers

When you call PostQuitMessage, you pass an exit code, not a message ID. PostQuitMessage will in turn generate (and post) the WM_QUIT message for you.

So technically, the difference is that with PostQuitMessage(WM_QUIT), the exit code will be 0x0012 (or 18 in decimal). Whereas PostQuitMessage(0) will provide an exit code of 0.

When either GetMessage and PeekMessage see a WM_QUIT message, they will return zero, and you can check for the exit code in the WPARAM part of the LPMSG parameter.

To return the exit code (the value you pass to PostQuitMessage) the message pump for your application could be something like this:

MSG msg;
while (0 != GetMessage(&msg, 0, 0, 0)) {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}

return msg.wParam;
like image 175
icabod Avatar answered Apr 11 '26 06:04

icabod


We can find code, on which the message loop is discontinued on an "error" of -1 from GetMessage(). So if the application in this case suddenly disappears, no shocked user will find out what the reason for this was. The better way is to keep the app running. And also a "bad" message does not damage the DefWindowProc() function.

like image 43
guestav Avatar answered Apr 11 '26 06:04

guestav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!