Search code examples
c++capiwinapishellexecute

how to get return value of an exe called by ShellExecute


How to get the return value of an exe which is called by shellexecute function.

ShellExecute(NULL, NULL, TEXT ( ".\\dpinstx86.exe" ), NULL, NULL, SW_SHOWNORMAL);

In the above example I want the return value of "dpinstx86.exe".


Solution

  • Use ShellExecuteEx instead to get the process handle, and GetExitCodeProcess to get the exit code.

    SHELLEXECUTEINFO ShExecInfo = {0};
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    ShExecInfo.lpFile = "c:\\MyProgram.exe";        
    ShExecInfo.lpParameters = "";   
    ShExecInfo.lpDirectory = NULL;
    ShExecInfo.nShow = SW_SHOW;
    ShExecInfo.hInstApp = NULL; 
    ShellExecuteEx(&ShExecInfo);
    WaitForSingleObject(ShExecInfo.hProcess,INFINITE);