Using WM_DESTROY PostQuitMessage

Correctly Closing Windows Applications in Win32 Menu Programming

© Guy Lecky-Thompson

Jul 9, 2008
Quit Key, Microsoft Clipart Collection
How to use WM_ Message processing, menu identifiers and the PostQuitMessage function correctly to perform shutdown operations in a Win32 application.

Editor's Choice

This Windows programming tutorial introduces the correct way to shut down a Windows application. The reader should be familiar with the following topics before proceeding:

The basic premise is that Win32 programming is all about processing messages that are sent to the application by Windows. In return, the application can send certain messages that inform parts of it about the state of the application in general.

Part of this message flow includes WM_CREATE, sent when the application starts up, and WM_DESTROY, sent when the application is shutting down. In addition, Win32 programmers will remember that the message loop in the WinMain function also continues to process until GetMessage returns false. It does so when it encounters the WM_QUIT message in the message queue.

So, the quickest way to ensure that the application shuts down would appear to be just to send it the WM_QUIT message. If that were true, there would be no need for this Windows programming tutorial. In fact, there are quite a few things that are required of a Windows application that make shutting down a little more complex.

WM_DESTROY and WM_CLOSE

Typically, an application processes the WM_DESTROY message when it is in the process of closing down. At the point at which the message is processed, it can be assumed that all child windows still exist, and so is a good place to perform last minute 'Are You Sure?' style cleanup tasks, and release system resources.

A better way to do this is to intercept the WM_CLOSE message, and use it as an opportunity to check whether the application should really close down or not. If the decision is to continue closing, then the DestroyWindow function should be called, which will send the WM_DESTROY message to the application.

Applications should return 0 if they process either of these messages.

PostQuitMessage and the WM_QUIT Message

The last thing that an application should do is call the PostQuitMessage function, with, as parameter, the return code (usually 0) that they wish to return. Windows will then send the WM_QUIT message to the application, which will cause the GetMessage function, called in WinMain, to return false, thus exiting the message loop.

Applications should never send the WM_QUIT message explicitly, and they should not process it, as it may never reach the message processing stage of the application.

Processing WM_COMMAND Menu Identifiers

If the program has both a close box, and an exit function accessible from the menu, this presents a bit of a quandry. If the close button is pushed, then the application is closed straight away - any opportunity for a last-minute cleanup is therefore lost.

For this reason, it is common for the programmer to use the WM_COMMAND menu identifier processing to send the WM_CLOSE message to the application, and trigger the shutdown process that way. Then, specific user-oriented confirmation can be sought in the WM_CLOSE processing, followed by cleanup in the WM_DESTROY processing, which then ends by calling PostQuitMessage(0).

This is the tidiest way to shut down the application.


The copyright of the article Using WM_DESTROY PostQuitMessage in Windows Programming is owned by Guy Lecky-Thompson. Permission to republish Using WM_DESTROY PostQuitMessage in print or online must be granted by the author in writing.


Quit Key, Microsoft Clipart Collection
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo