Why is CWinAppEx::OnFileNew called automatically?

  • Thread starter Thread starter linganmm
  • Start date Start date
L

linganmm

Guest
And I just override the default CWinAppEx::OnFileNew method by

Hi, I am using Visual Studio 2015 to develop a single document MFC program.


void CmyappApp::OnFileNew()
{
CWinAppEx::OnFileNew();
MessageBox(NULL, _T("hello.\n"), _T("Error"), MB_OKCANCEL);
}


Then I find that the method above is called automatically upon executing the program, because I see the Message box immediately after the program window shows up.

I traced the stack and found that the onFileNew method gets called in CmyappApp::InitInstance by 2 functions:


1)ProcessShellCommand(cmdInfo), in which cmdInfo.m_nShellCommand = CCommandLineInfo::FileNew by default.


2) m_pMainWnd->UpdateWindow();


I don't what UpdateWindow() actually does, however if ProcessShellCommand is skipped by, for example, setting cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing, the following exception will be thrown by UpdateWindow():


Exception thrown at 0x101CCC0E (mfc140ud.dll) in mrteditor.exe: 0xC0000005: Access violation reading location 0x00000020.


Finally I have to adopt the following workaround:


first, declare a flag initFlag which is initialized to true in the constructor of CmyappApp.


second, set initFlag=false at the end of CmyappApp::InitInstance().


Third, kick the routine into different branches according to the value of initFlag:


void CmyappApp::OnFileNew()
{
if (initFlag) {
CWinAppEx::OnFileNew();
return;
}
// my codes go here
}


But this way looks dirty:-( Is there any more elegant way to resolve this?

Thank you!

Continue reading...
 
Back
Top