15

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

2vision2
  • 4,651
  • 14
  • 74
  • 156
  • 1
    I think what you mean by "return value" is the output from CMD, which this question addresses: http://stackoverflow.com/questions/469152/using-shellexecuteex-and-capturing-standard-in-out-err. – Ben Nov 12 '12 at 00:49

1 Answers1

22

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);
Warren P
  • 58,696
  • 38
  • 168
  • 301
kol
  • 24,444
  • 11
  • 70
  • 104
  • 3
    Don't forget to *wait* on the handle. – Hans Passant Jun 05 '12 at 11:58
  • 2
    http://www.codeproject.com/Articles/1842/A-newbie-s-elementary-guide-to-spawning-processes ...and call GetExitCodeProcess() with the hProcess member of SHELLEXECUTEINFO. – kol Jun 05 '12 at 12:06
  • 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? – 2vision2 Jun 06 '12 at 10:45
  • @Hans - for clarification, *wait* meaning `WaitForSingleObject()`, I hope (new to this so please correct me if I'm wrong...) – Ben Nov 12 '12 at 00:41
  • 3
    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. – David Heffernan Mar 20 '16 at 10:39