Return Values
If the function retrieves a message other than WM_QUIT, the return value is nonzero.
If the function retrieves the WM_QUIT message, the return value is zero.
If there is an error, the return value is -1. For example, the function fails if hWnd is an invalid window handle or lpMsg is an invalid pointer. To get extended error information, call GetLastError.
Warning Because the return value can be nonzero, zero, or -1, avoid code like this:
while (GetMessage( lpMsg, hWnd, 0, 0)) ...
The possibility of a -1 return value means that such code can lead to fatal application errors. Instead, use code like this:
BOOL bRet;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
博客探讨了在Win32汇编中GetMessage函数的使用,强调了其返回值的重要性。文章指出,由于返回值可能为非零、零或负一,直接用while循环判断可能导致应用错误。正确的做法是使用BOOL变量进行错误处理。文中提供了MSDN的官方解释,并给出了示例代码来避免潜在的错误。
1098

被折叠的 条评论
为什么被折叠?



