Click to See Complete Forum and Search --> : Running AND tracking other executables from C++


sedavidw
June 1st, 2010, 03:55 PM
So I've got some code that I'm writing that I want to call another .exe file. I don't have access to the source of that .exe and there is no dll form of it so I'm stuck with what I've got. So I want to make a call to run that code (which I do know how to do).

But I need the code I'm writing to wait for the other executable to be done running before it continues (and unfortunately there's a LOT of variation in its run time). Is there any ways to use API calls to cause some sort of wait loop to wait for another executable to be done?

hoxsiew
June 1st, 2010, 04:06 PM
Use ShellExecuteEx with SHELLEXECUTEINFO set to return the handle to the running app, then wait on that handle:



SHELLEXECUTEINFO sei;

memset(&sei,0,sizeof(SHELLEXECUTEINFO));
sei.cbSize=sizeof(SHELLEXECUTEINFO);
sei.fMask=SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb=L"OPEN";
sei.lpFile=L"C:\\Program Files (x86)\\MyProgram\\MyProgram.exe";
sei.lpDirectory=L"C:\\Program Files (x86)\\MyProgram\\";
sei.nShow=1;

ShellExecuteEx(&sei);
WaitForSingleObject(sei.hProcess,INFINITE);