MFC application crashes in ProcessShellCommand () when opening a file specified on the command line

The problem I need to solve is to use the MFC function ProcessShellCommand()in InitInstance() CWinAppto process the Open file with a specific path when the application to open the file is launched by another application.

I have an MFC MDI (Multiple Document Interface) application that is launched by another command-line application, using ShellExecute(), containing the path to the file to open. When compiling with Visual Studio 2005, I do not see a problem with the running application. When compiling with Visual Studio 2013, an application that crashes and I never see the application window.

Starting in the debugger, I see a dialog box with an error that has the name "Microsoft Visual C ++ Runtime Library" with the error message "Debugging error with error!". setting the mfc120ud.dll file and the src \ mfc \ filelist.cpp line file: 221

At this point, I can connect to the application, then click the Retry button in the dialog box. Then, as I continue, I see a Visual Studio error dialog from an unhandled exception that is apparently being thrown KernelBase.dll.

Unhandled exception in 0x76EBC54F in NHPOSLM.exe file: Microsoft C ++ Exception: CInvalidArgException in memory location 0x0014F094.

If I click the Continue button, this time I get another "Debug check failed" from the line src \ mfc \ filelist.cpp: 234

Sleep() Debug->Attach to process Visual Studio 2013 .

- , ProcessShellCommand() , , set, . , , .

ProcessShellCommand View Frame Windows, :

, ProcessShellCommand() , . , , .

, , , ProcesShellCommand() , .

CCommandLineInfo cmdInfo;

if( !ProcessShellCommand( cmdInfo ) )
    return FALSE;

ParseCommandLine( cmdInfo );

if( cmdInfo.m_nShellCommand != CCommandLineInfo::FileNew )
{
    if (!ProcessShellCommand( cmdInfo ) )
        return FALSE;
}

, , , . , , MFC SDI (Single Document Interface) MFC- MDI (Multiple Document Interface), , , New, Open.

, , , , , . , , .

, , - , .

ProcessShellCommand() . CWinApp:: ProcessShellCommand, :

  • InitInstance CCommandLineInfo ParseCommandLine.
  • ParseCommandLine CCommandLineInfo::ParseParam , .
  • ParseParam CCommandLineInfo, ProcessShellCommand.
  • ProcessShellCommand .

, InitInstance(), :

// Register the application document templates.  Document templates
//  serve as the connection between documents, frame windows and views.

CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
    IDR_NEWLAYTYPE,
    RUNTIME_CLASS(CNewLayoutDoc),
    RUNTIME_CLASS(CChildFrame), // custom MDI child frame
    RUNTIME_CLASS(CNewLayoutView/*CLeftView*/));
AddDocTemplate(pDocTemplate);

// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
    return FALSE;
m_pMainWnd = pMainFrame;

// Parse command line for standard shell commands, DDE, file open
CLOMCommandLineInfo cmdInfo;
/*initialize language identifier to English so we wont have garbage if no language 
flag is set on teh command line*/
cmdInfo.lang = LANG_ENGLISH;
cmdInfo.sublang = SUBLANG_ENGLISH_US;
//CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);

BOOL success = pMainFrame->ProcessCmdLineLang(cmdInfo.lang, cmdInfo.sublang);
if(!success){
    AfxMessageBox(IDS_CMDLINE_LANG_NF,MB_OK,0);
}
// Dispatch commands specified on the command line
if (!ProcessShellCommand(cmdInfo))
    return FALSE;

// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(SW_SHOWNORMAL);
pMainFrame->UpdateWindow();

, ProcessShellCommand() , . , MDI. , VS 2005 VS2013.

, codeproject, Debug Assertion Error Visual Studio 2010, , , src\mfc\filelist.cpp, , .

cmdInfo, , (*((CCommandLineInfo*)(&(cmdInfo)))).m_strFileName, L "C:\Users\rchamber\Documents\ailan_221.dat". , , ShellExecute().

. . , L "C:\\Users\\rchamber\\Documents\\ailan_221.dat", , -, , .

3/23/2016 -

. Visual Studio 6.0, Visual Studio 2005. InitInstance() CWinApp , .

+4
2

Visual Studio 2013 MFC MDI (Multiple Document Interface) , , , , .

, -, COM. InitInstance() , . - COM.

// InitCommonControlsEx() is required on Windows XP if an application
// manifest specifies use of ComCtl32.dll version 6 or later to enable
// visual styles.  Otherwise, any window creation will fail.
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
// Set this to include all the common control classes you want to use
// in your application.
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);

CWinApp::InitInstance();

// Initialize OLE libraries
if (!AfxOleInit())
{
    AfxMessageBox(IDP_OLE_INIT_FAILED);
    return FALSE;
}

AfxEnableControlContainer();

// AfxInitRichEdit2() is required to use RichEdit control   
// AfxInitRichEdit2();

Visual Studio 2005 , Visual Studio 2005 Visual Studio 2013 . Visual Studio 2005, Visual Studio 2005.

Visual Studio 2005 MFC MDI , .

+1

Windows 10 Visual Studio 2013 MDI. ProcessShellCommand() - . , , . . CoInitialize(), ( - ):

// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
 return FALSE;
m_pMainWnd = pMainFrame;

// The main window has been initialized, so show and update it.
// This needs to be up really before parsing the command line,
// so that any FileOpen command has something to render in.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();

// Parse command line for standard shell commands, DDE, file open
CCommandLineInfo cmdInfo;

ParseCommandLine(cmdInfo);
if(!ProcessShellCommand(cmdInfo))
  return FALSE;
0

All Articles