MFC application crashing in ProcessShellCommand() when file to open specified on command line

前端 未结 3 498
隐瞒了意图╮
隐瞒了意图╮ 2021-01-19 18:03

The problem I need to solve is how to use the MFC function ProcessShellCommand() in the InitInstance() of a CWinApp to process a File

3条回答
  •  灰色年华
    2021-01-19 18:38

    I just had the same problems in Windows 10 with Visual Studio 2013 and an MDI app. Here, the provided solution of calling ProcessShellCommand() twice still resulted in a crash. The solution was to create the window earlier than interpreting the command line. That worked for me. I tried the CoInitialize() variant and that also works (put it somewhere before the code below):

    // 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;
    

提交回复
热议问题