The message box is not displayed with the ExitInstance function of a dialog-based MFC application (253130)



The information in this article applies to:

  • Microsoft Visual C++ 4.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 4.2
  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 4.2
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q253130
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code. Microsoft Visual C++ 2005 supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model.

SYMPTOMS

When you try to display a message box in the ExitInstance function of a dialog-based Microsoft Foundation Classes (MFC) application, you don't see the message box displayed. Also, if you place any window creation code after the DoModal call in your InitInstance function, you don't see the window.

CAUSE

Destruction of the main window causes a WM_QUIT message to be posted to the main application thread indicating to the operating system that the thread is to be terminated. Any window created after this is destroyed immediately after creation resulting in a brief flash.

RESOLUTION

To work around this problem, comment out the line in the InitInstance function that assigns the m_pMainWnd function to your dialog as follows:
  // m_pMainWnd = &dlg;
				

Also, and instead of using message boxes, you could use the MFC TRACE macros to display information in the debug output window for debugging purposes.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a new MFC dialog-based application called "Dlgbased".
  2. Add the following code to the InitInstance function:
    BOOL CDlgbasedApp::InitInstance()
    {
       int ret;
       ret = AfxMessageBox("Before Dialog");
    
       CDlgbasedDlg dlg;
       m_pMainWnd = &dlg;
    
       int nResponse = dlg.DoModal();
       if (nResponse == IDOK)
       {
       }
       else if (nResponse == IDCANCEL)
       {
       }
    
       ret = AfxMessageBox("After Dialog.  Won't see me");
       return FALSE;
    }
    						
  3. Use ClassWizard to override the ExitInstance function for CdlgbasedApp.
  4. Add the following code to the ExitInstance function:
    BOOL CDlgbasedApp::ExitInstance()
    {
       AfxMessageBox("Won't see me!");
       return CWinApp::ExitInstance();
    }
    						
  5. Build and run the application.

After dismissing the dialog box, notice that the message boxes do not appear.

Modification Type:MajorLast Reviewed:1/7/2006
Keywords:kbtshoot kbDlg kbprb KB253130 kbAudDeveloper