What Happens When GetMessage Fails?
If GetMessage (...) fails, isn't the message removed from the message queue? I ask because when I have the next loop I end up going into an infinite loop trying to process the same message over and over again:
while( GetMessage( &msg, NULL, WM_SPEVENT, WM_SPUUIEVENT ))
{
//Do something with my message. (Translate + dispatch perhaps, if I wanted)
}
Because I am not handling the GetMessage error case, will the loop loop on the same message over and over again? Is that why the following path is the correct way to implement a loop ?:
while( (bRet = GetMessage( &msg, NULL, WM_SPEVENT, WM_SPUUIEVENT )) != 0)
{
if (bRet == -1)
{
//
}
else
{
//Do something with my message
}
}
source to share
If GetMessage
it fails and returns -1
, then usually your first block of code will indeed result in an infinite loop. And so if -1
is a possible return value, which is why you see MSDN samples of the form for your second block of code.
However
GetMessage(&msg, NULL, WM_SPEVENT, WM_SPUUIEVENT)
never breaks and therefore never returns -1
. The failure modes for GetMessage
are that &msg
refer to invalid memory or where the second parameter, the window handle, is invalid. There can be no failure mode here, assuming, of course, that it is msg
correctly defined. This is usually a local variable, so it's &msg
always good.
Raymond Chen talked about it here: When will GetMessage return -1?
source to share