A Negative Experience in Getting Messages

Published January 14, 2011
Advertisement
Recently WinDevs, the guys in charge of developing the Getting Started content for native developers on MSDN, and I had a short conversation relating to message loops. They're in the process of creating a Win32 tutorial in the vein of TheForgers teaching the basics of creating a small Direct2D drawing app. The message loop used in their tutorials is the ubiquitous:


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
0 likes 2 comments

Comments

Aardvajk
Hmm.

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.
January 14, 2011 08:03 PM
adeyblue
I've also never seen a message loop written like that outside of that page. When I first wrote this it was due to idle curiosity after it struck me that the MSDN function docs implore you to do it the verbose way, but the MSDN tutorial docs explain and do it the common way. A 'does it really matter?' if you will. It was clear that it wouldn't due to the reasons you describe, and the sheer ubiquitousness of it.

As for my not entirely trusting MSDN: looking at my community content, there are 5 cases where the docs are either flat out wrong or under specify what's required of code [url="http://msdn.microsoft.com/en-us/library/ms648003(v=VS.85).aspx#2"]1[/url], [url="http://msdn.microsoft.com/en-us/library/aa379626(v=VS.85).aspx#1"]2[/url], [url="http://msdn.microsoft.com/en-us/library/aa365468(v=VS.85).aspx#1"]3[/url], [url="http://msdn.microsoft.com/en-us/library/bb773257(v=VS.85).aspx#2"]4[/url], [url="http://msdn.microsoft.com/en-us/library/ms232092(v=VS.90).aspx#1"]5[/url].

If you include the invalid message pointer case from this article, perfectly fine code as conditioned by the docs would give you two crashes, a buffer with the wrong format, "success" when an error occurs and time wasted guessing at adequate permission levels. I don't question that it's better documentation than a lot of libraries offer, but what it presents is an ideality which isn't always reflected in the product it describes.
January 15, 2011 02:22 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement
Advertisement