MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
On a forum I visit, one of the denizens had pointed out in response to an unrelated query that the GetMessage documentation states -1 can be returned on failure and that the loop above should not be used, instead advising programmers to use:
MSG msg;
BOOL bRet;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
As the tutorial is aimed at beginners and no doubt intended to teach best practices, I mentioned the contradictory advice to them. After a bit of investigation, they've since explained the incongruity. MSDN mentions as examples that an invalid HWND and an invalid message pointer can be causes of a -1 return. Being curious about such things and not entirely trusting of MSDN's words, I wondered what exactly causes such a return...
Read the rest of the entry on Just Let It Flow
While I suppose that the advice on MSDN is technically correct, you can be fairly sure, due to the sheer locality of code, that your HWND and pointer to MSG are valid.
I've never come across a message loop that works the way they suggest. I guess maybe it [i]should[/i] look like that. I suppose that, since Win32 API is not able to use exceptions, we should really be checking the possibility of an error return from any function that [i]could[/i] be returning an error.
Classic example of why modern languages use more advanced features to communicate errors I guess. While I agree with the advice on MSDN from a technical viewpoint, I'm not planning to change my own code.
There are, I would think, hundreds of thousands of applications that people pay money for and rely on that don't bother to check this. That doesn't mean they are right though.
I'd question why you are not entirely trusting of MSDN's words though. I've always found their documentation faultless - something I can't say for [i]any[/i] other library I've used, including some with significant profile which I suppose is due to the time/money they have invested in getting it right.