What is the correct way to programmatically quit an MFC application?

User picture User · Sep 27, 2011 · Viewed 30.8k times · Source

Using windows MFC C++. I have a third party app that calls a user-defined method in my CWinApp derived class. This method is called after InitInstance(). If there is an error in this method, such that an exception is thrown and caught in a try/catch block, I would like to exit the application from the catch block. What is the canonical and correct way to quit?

UPDATE:

Serge I believe is right that in InitInstance() returning false is the correct way to quit the application. However, now suppose I want to quit from a CDialog derived class's OnInitDialog() handler, what's the correct way to do that.

UPDATE 2

For me, I found calling PostMessage(WM_CLOSE) to be the best way from my non-modal CDialog derived class. All other methods of quitting I tried would raise some exception or other in some circumstances.

Here's an example of how I use it:

BOOL SomeDialog::OnInitDialog()
{
    CDialog::OnInitDialog();

    ::OleInitialize(nullptr);

    try
    {
        // ...load settings file here
    }
    catch(...)
    {
        PostMessage(WM_CLOSE);
        return TRUE;
    }

    // return TRUE  unless you set the focus to a control
    return TRUE;
}

Answer

Serge Wautier picture Serge Wautier · Sep 27, 2011

In InitInstance()

Exiting the app while you are still in InitInstance(): Simply return FALSE from InitInstance().

In the main message loop

It's another story though if you are already in the message loop: The standard way to close an app is to exit the message loop:

PostQuitMessage(0), as its name implies, posts a WM_QUIT message. The message loop reacts by exiting the loop and closing the program.

But you shouldn't simply do that: You should close the opened windows in your app. Assuming you have only your main window, you should destroy it by calling

m_pMainWindow->DestroyWindow();

MFC will react by PostQuitMessage() for you, hence exit the main message loop and close your app.

Better yet, you should post a WM_CLOSE to let your main window close gracefully. It may for example decide to save the current document. Beware though: the standard OnClose() handler may prompt user to save dirty documents. User can even cancel the close action using this prompt (Save document? Yes, No, Cancel).

Destroying the main window will post a WM_DESTROY message to it. MFC reacts by calling PostQuitMessage(0) to exit the message pump. (Actually, MFC does the call in OnNcDestroy() since WM_NCDESTROY which is the absolute last mesage received by a window)

Dialog-based app

Call EndDialog(-1); // Or replace -1 by IDCANCEL, whatever

This call, as you probably know, will close the dialog.

Note that the main dialog of dialog-based app executes in InitInstance(). Closing the dialog will simply exit InitInstance(), which always returns FALSE in such projects.