2

Let say I want the program "C:\MyProgram.exe" to run with parameters that are two variables. The problem that occurs is that MyProgram only receives 2 parameters, while I clearly pass 3 parameters.

My code:

SHELLEXECUTEINFO    ShExecInfo = { 0 };
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;

ShExecInfo.lpFile = T("\"C:\\MyProgram.exe\"");
ShExecInfo.lpParameters = _T("\"\"") _T(" ") + dir + file[i] + _T(" ") + dir + outputfile + _T(".TIFF");
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess, 1500);
if(GetExitCodeProcess(ShExecInfo.hProcess, &exitCode)){
    MessageBox(_T("Conversion ") + file[i] + _T(" unsuccesful."), _T("TEST!"), MB_OK);
    succes = 0;
}

Because there isn't much info on the internet on variable parameters with ShellExecuteEx, I could not find a proper explanation for this.

Does any of you maybe know how to solve this issue? Thanks in advance!

moffeltje
  • 4,521
  • 4
  • 33
  • 57

2 Answers2

2

Simply because your construct yields into a temporary object and the pointer to it (a CString I guess) is stored but the temporary object is already destroyed when you launch the program.

auto str = _T("\"\"") _T(" ") + dir + file[i] + _T(" ") + dir + outputfile + _T(".TIFF");
ShExecInfo.lpParameters = str;
ShellExecuteEx(&ShExecInfo);
xMRi
  • 14,982
  • 3
  • 26
  • 59
  • Thanks, this worked for me. What does the 'auto' keyword do exactly? Does it determine the datatype for me? – moffeltje Mar 17 '15 at 11:08
  • 1
    Yes. http://www.codeproject.com/Articles/570638/Ten-Cplusplus-Features-Every-Cplusplus-Developer – xMRi Mar 17 '15 at 11:18
0

Did you check those two parameters that MyProgram receives, what values do they have?

The problem is most likely this piece of code:

ShExecInfo.lpParameters = _T("\"\"") _T(" ") + dir + file[i] + _T(" ") + dir + outputfile + _T(".TIFF");

You didn't say what types dir or file[i] have, but in general adding C style strings like this (TCHAR[] or TCHAR* are still C style strings) doesn't concatenate them, if that's what you expect to happen in this case.

Check what ShExecInfo.lpParameterscontains after that assignment, by displaying it or preferably with the debugger.

Ionut
  • 6,436
  • 1
  • 17
  • 17