Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
How to Programatically Terminate an MFC Application
Article ID: 117320 - View products that this article applies to.
This article was previously published under Q117320
Under certain conditions, you may want to terminate an MFC application programatically. MFC does not provide a public function to gracefully exit an application.
A method for dealing with this is to create a function in your application like the following:
As you can see, this is implemented as a global function, which can be called from anywhere in your application. It simply sends a WM_CLOSE message to your application's mainframe window. This initiates an orderly shutdown of the application.
If you are using MFC, version 2.5 or later, you can take advantage of a new global MFC function, "AfxGetMainWnd", to simplify the code:
NOTE: Always call CDocument::SetModifiedFlag() after changing your document data. This will ensure that the framework prompts the user to save before shutdown. If you need more extensive control over the shutdown procedure, you can override CDocument::SaveModified().
Article ID: 117320 - Last Review: November 21, 2006 - Revision: 3.1