问题描述
根据, MSDN库,当有错误时,它可能返回-1。该文档提供了应该避免的常见错误的代码段:
According to the GetMessage API from MSDN Library, it might returns -1 when there is an error. The document provides a code snippet of common error that should be avoid:
while (GetMessage( lpMsg, hWnd, 0, 0)) ...
文件说:
-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);
}
}
我的问题是,在每个示例代码中,应用程序从Visual Studio创建,从Microsoft,主消息循环如下所示:
My question is, in every sample code, including default application created from Visual Studio, from Microsoft, the main message loop looks like below:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
注意上面的GetMessage的第二个参数是NULL。如果上面的代码是有效的,这是否意味着GetMessage这里将不会返回-1,所以处理返回值-1是不必要的?
Notice that the 2nd parameter of GetMessage above is NULL. If the code above is valid, does this mean that GetMessage here will NEVER returns -1 so that dealing with return value of -1 is not necessary?
推荐答案
由于默认情况下VS给你错误的代码,没有人会在乎,这很可能在当前版本的Windows 中不会造成麻烦。
Given that VS gives you wrong code by default, and nobody seems to care, it is very likely that this causes no trouble in current versions of Windows.
GetMessage
的某个未来版本可能会返回-1。然而,由于错误代码现在必须在如此多的现有应用中,这将打破大量的现有代码。鉴于微软致力于向后兼容性,我认为他们不太可能改变这么多程序依赖的 GetMessage
的行为。
尽管如此,你仍然应该遵循文档。
And in spite of all that, you should still follow the documentation.
这篇关于将GetMessage()返回-1在主消息循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!