With my MFC application, I wanted something similar, but I found that the accepted answer was only a partial solution for me. If the file name is specified on the command line of the MFC application when it starts, using the accepted answer will not open the file.
I wanted (1) to allow the file to open when the MFC application was launched from the command line and (2) to change the current working folder.
In InitInstance()overriding the application that is being distributed CWinAppEx, I used the following source:
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, 0, CMFCApplication4Doc::m_UserDocumentsFolder))) {
PathAppend(CMFCApplication4Doc::m_UserDocumentsFolder, L"GenPOS BO");
TRACE1("home path found %s\n", CMFCApplication4Doc::m_UserDocumentsFolder);
if (!CreateDirectory(CMFCApplication4Doc::m_UserDocumentsFolder, NULL)) {
DWORD dwLastError = GetLastError();
if (dwLastError != ERROR_ALREADY_EXISTS) {
TRACE1("CreateDirectory error %d\n", dwLastError);
}
}
SetCurrentDirectory(CMFCApplication4Doc::m_UserDocumentsFolder);
}
else {
TRACE0("home path not found");
}
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew)
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
if (!ProcessShellCommand(cmdInfo))
return FALSE;
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
source
share