根据MSDN Library中的GetMessage API,当出现错误时,它可能会返回-1.该文档提供了应避免的常见错误的代码片段:
while (GetMessage( lpMsg, hWnd, 0, 0)) ...
Run Code Online (Sandbox Code Playgroud)
文件说:
-1返回值的可能性意味着此类代码可能导致致命的应用程序错误.相反,使用这样的代码:
BOOL bRet;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,在每个示例代码中,包括从Microsoft Visual Studio创建的默认应用程序,主消息循环如下所示:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Run Code Online (Sandbox Code Playgroud)
请注意,上面的GetMessage的第二个参数是NULL.如果上面的代码有效,这是否意味着此处的GetMessage将永远不会返回-1,因此不需要处理返回值-1?