how to get return value of an exe called by ShellExecute

2vision2 picture 2vision2 · Jun 5, 2012 · Viewed 23k times · Source

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

Answer

kol picture kol · Jun 5, 2012

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);