You are currently offline, waiting for your internet to reconnect

How to Programatically Terminate an MFC Application

This article was previously published under Q117320
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
Under certain conditions, you may want to terminate an MFC applicationprogramatically. MFC does not provide a public function to gracefully exitan application.
A method for dealing with this is to create a function in your applicationlike the following:
   void ExitApp()   {        // same as double-clicking on main window close box        ASSERT(AfxGetApp()->m_pMainWnd != NULL);        AfxGetApp()->m_pMainWnd->SendMessage(WM_CLOSE);   }				
As you can see, this is implemented as a global function, which can becalled from anywhere in your application. It simply sends a WM_CLOSEmessage to your application's mainframe window. This initiates an orderlyshutdown of the application.

If you are using MFC, version 2.5 or later, you can take advantage of a newglobal MFC function, "AfxGetMainWnd", to simplify the code:
   void ExitMFCApp()   {        // same as double-clicking on main window close box        ASSERT(AfxGetMainWnd() != NULL);        AfxGetMainWnd()->SendMessage(WM_CLOSE);   }				
NOTE: Always call CDocument::SetModifiedFlag() after changing your documentdata. This will ensure that the framework prompts the user to save beforeshutdown. If you need more extensive control over the shutdown procedure,you can override CDocument::SaveModified().
kbinf 1.00 1.50 1.51 1.52 2.00 2.10 2.50 2.51 2.52 3.00 3.10 4.00

Article ID: 117320 - Last Review: 11/21/2006 16:06:05 - Revision: 3.1

  • Microsoft Foundation Class Library 4.2
  • KB117320