0

I'm trying to figure out how to pass filename from within an existing executable to a newly generated executable of same type & then the new exe load said file name. Following is something I'm working on but I'm bit lost really.

CString cstrExePathLoc;
GetModuleFileName(NULL, cstrExePathLoc.GetBuffer(MAX_PATH), MAX_PATH);
wchar_t szCommandLine[1024] = _T("C:\\Users\\Home\\Desktop\\testfile.tmp");

PROCESS_INFORMATION processInfo;
STARTUPINFO startupInfo;
::ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
CreateProcess(
  cstrExePathLoc, szCommandLine, NULL, NULL, FALSE, CREATE_DEFAULT_ERROR_MODE, NULL, NULL,
  &startupInfo, &processInfo
);

EDIT: this still doesn't open the file. A new ExeApp is started but no file is loaded. No errors generated at all.

I've searched net but no examples I've come across clearly explain how to do this. Any help would be appreciated. Thanks.

EDIT: simple solution here that's worked thanks to Robson Filho Colodeti below.

CString cstrExeFilePathAndFilePath2Open = cstrExePathLoc;
cstrExeFilePathAndFilePath2Open += L" \"";
cstrExeFilePathAndFilePath2Open += cstrFilePath2Open;
cstrExeFilePathAndFilePath2Open += L"\"";
CreateProcess(csExePath, cstrExeFilePathAndFilePath2Open.GetBuffer(0), NULL, NULL, TRUE, NULL, NULL, NULL, &sui, &pi);
ReturnVoid
  • 1,106
  • 1
  • 11
  • 18
  • Pass file name in `lpCommandLine` parameter of `CreateProcess`. In MFC application you can access command line using `CWinApp::m_lpCmdLine` member. In Console application it is available in `argc, argv` main function parameters. – Alex F Nov 05 '14 at 08:28
  • Normally you use ShellExecute to start new processes, then just pass whatever command line arguments you want. – AndersK Nov 05 '14 at 08:31
  • can all that be done in the one function, or do I need to call that from elsewhere & sent elsewhere? I'll edit my question little bit, as passing filepath to lpCommandLine isn't opening file. – ReturnVoid Nov 05 '14 at 08:45
  • @ReturnVoid Did you write code for reading command line parameters and opening the file? – atoMerz Nov 05 '14 at 08:55

1 Answers1

2

Opening the other program

Using CreateProcess

you are in the right way, you can use the CreateProcess method.

BOOL fSuccess;
CString csDir = L"c:\your\working\directory\";
CString csParameters = L"parameter1 parameter2 parameter3 /parameter4=value";
CString csCommand = L"c:\folder\of\the\executable\executable.exe";
csCommand+= L" ";
csCommand+= csParameters;
// Create the child process.
fSuccess = CreateProcess(NULL, csCommand.GetBuffer(0), NULL, NULL, TRUE, 0, NULL,
             csDir, &startupInfo, &processInfo);

Using ShellExecute

an easier way is to use the ShellExecute method because the create process method is a more "advanced" way to call a process since it gives you a lot of possibilities to control the results etc...

Reading the parameters inside the other program

then you will have to read these parameters from the other executable: check this thread

Community
  • 1
  • 1
Robson
  • 916
  • 5
  • 22
  • i think that if you do a command like "c:\folder\executable.exe c:\folder2\filename.extension" if the other application is a MFC application i think that mfc will automatically generate the file open call. in the InitiInstance method of the other application you will find something like " CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo);" and that actually does all the work. so if you only need to open the file then i think you don't have to overwrite any of those things – Robson Nov 05 '14 at 09:21
  • In my program MyApp::InitInstance() function contains ParseCommandLine(cmdInfo);...wait, so I can just use szCommandLine to pass both *.exe & filepath? – ReturnVoid Nov 05 '14 at 09:23
  • yes you can use it, at least i am doing it in my application XD but i guess the correct way should be using all the parameters =) – Robson Nov 05 '14 at 09:28