winmain() function / c++
-
24-03-2021 - |
Вопрос
can you please explain to me the significance of this statement generally written at the end of the WinMain()
function:
return (int) msg.wParam;
Im used to ending my console applications with return 0;
is it wrong for windows applications?
Thank you.
Решение
The GetMessage()
loop stops after receiving a WM_QUIT
window message which is normally issued after a call to the PostQuitMessage()
function, which describes its nExitCode
parameter as the application exit code.
If you want to respect the intent of the person who invoked the PostQuitMessage()
function, you should return that value from main. This is what you are doing when you return the wParam
retrieved from the last invocation of GetMessage()
.
Другие советы
From the WinMain()
docs (http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559.aspx):
Terminate the message loop when it receives a
WM_QUIT
message. At that point, yourWinMain
should exit the application, returning the value passed in theWM_QUIT
message'swParam
parameter. IfWM_QUIT
was received as a result of callingPostQuitMessage
, the value ofwParam
is the value of thePostQuitMessage
function'snExitCode
parameter.
From the first hit of a Google search
Your WinMain should initialize the application, display its main window, and enter a message retrieval-and-dispatch loop that is the top-level control structure for the remainder of the application's execution. Terminate the message loop when it receives a WM_QUIT message. At that point, your WinMain should exit the application, returning the value passed in the WM_QUIT message's wParam parameter. If WM_QUIT was received as a result of calling PostQuitMessage, the value of wParam is the value of the PostQuitMessage function's nExitCode parameter. For more information, see Creating a Message Loop.