how to get return value of an exe called by ShellExecute

24,785

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);
Share:
24,785
2vision2
Author by

2vision2

Updated on February 12, 2020

Comments

  • 2vision2
    2vision2 over 4 years

    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".

  • user1703401
    user1703401 about 12 years
    Don't forget to wait on the handle.
  • kol
    kol about 12 years
    codeproject.com/Articles/1842/… ...and call GetExitCodeProcess() with the hProcess member of SHELLEXECUTEINFO.
  • 2vision2
    2vision2 about 12 years
    Dear Kol, Does the GetExitProcess returns the error code of the exe I'm calling in "ShellExecuteEx"?? i.e., does "lpExitCode" the second parameter of "GetExitProcess" contains the return value of "dpinst.exe" as per my example...and do I want to wait for the shellexecuteEx to complete?
  • Ben
    Ben over 11 years
    @Hans - for clarification, wait meaning WaitForSingleObject(), I hope (new to this so please correct me if I'm wrong...)
  • David Heffernan
    David Heffernan over 8 years
    You also leak the handle. Close it after you've waited on it. You also fail to check for errors. CreateProcess is a much better choice.