Question

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.

Was it helpful?

Solution

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().

OTHER TIPS

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, 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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top