The problem I need to solve is how to use the MFC function ProcessShellCommand()
in the InitInstance()
of a CWinApp
to process a File
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;