Microsoft KB Archive/253130

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

Article ID: 253130

Article Last Modified on 1/7/2006

-

APPLIES TO


 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 4.2 Enterprise Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 4.2 Professional Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



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.



Steps to Reproduce Behavior
 Create a new MFC dialog-based application called "Dlgbased".  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; }                        Use ClassWizard to override the ExitInstance function for CdlgbasedApp.  Add the following code to the ExitInstance function: BOOL CDlgbasedApp::ExitInstance {  AfxMessageBox("Won't see me!"); return CWinApp::ExitInstance; }                        Build and run the application.</li></ol>

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

Keywords: kbtshoot kbdlg kbprb KB253130

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.