1

I have an .exe which takes 2 arguments. I need to run the .exe file between my program in runtime. I have used ShellExecuteEx to run the .exe file but it is not taking the arguments. Please help where did i went wrong. I am posting my code below.

void StartProgram() {
    SHELLEXECUTEINFO lpExecInfo;
    lpExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    lpExecInfo.lpFile = L"F:\\EXEFolder\\RunMe.exe"; // Path to the .exe
    lpExecInfo.fMask = SEE_MASK_DOENVSUBST | SEE_MASK_NOCLOSEPROCESS;
    lpExecInfo.hwnd = NULL;
    lpExecInfo.lpVerb = L"open"; // to open  program
    lpExecInfo.lpParameters = L"D:/input.csv" "F:/output.csv;"; //Arguments to be passed to .exe
    lpExecInfo.lpDirectory = NULL;
    lpExecInfo.nShow = SW_SHOW;
    lpExecInfo.hInstApp = (HINSTANCE)SE_ERR_DDEFAIL;   
    ShellExecuteEx(&lpExecInfo);
    //wait until a file is finished printing
    if (lpExecInfo.hProcess != NULL)
    {
        ::WaitForSingleObject(lpExecInfo.hProcess, INFINITE);
        ::CloseHandle(lpExecInfo.hProcess);
    }
}
G93
  • 21
  • 1
  • 3

1 Answers1

6

The lpParameters member is a pointer to a space-delimited string of arguments, just like you would pass them in a command-line environment.

If you want to pass two arguments to the program, you do it like

lpExecInfo.lpParameters = L"argument1 argument2";

For your case it should be like

lpExecInfo.lpParameters = L"D:/input.csv F:/output.csv";

To explain what you do wrong, the C++ compiler have a phase where it concatenate adjacent string literals into a single string. That means L"D:/input.csv" "F:/output.csv;" will be concatenated into L"D:/input.csvF:/output.csv;" which is a single argument.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621