Board index » cppbuilder » How to run a Command in a progam and wait until finish?

How to run a Command in a progam and wait until finish?


2007-09-23 09:58:27 PM
cppbuilder64
I tried to google "ShellExecute wait" there are many different solutions
suggested. They all look a bit complicated. Is there another command for
this and can pause the main process?
thanks
Phuoc
--
===============================
I'm using BCB5 on XP Home
 
 

Re:How to run a Command in a progam and wait until finish?

Phuoc wrote:
Quote
I tried to google "ShellExecute wait" there are many different solutions
suggested. They all look a bit complicated. Is there another command for
this and can pause the main process?
Don't use ShellExecute() for this but CreateProcess() along with
WaitForSingleObject().
Something like this:
PROCESS_INFORMATION piProcInfo;
STARTUPINFO siStartInfo;
memset(&siStartInfo, NULL, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
//launch your application
CreateProcess(NULL, "yourApp.exe",
NULL, NULL, 0, 0, NULL, NULL,
&siStartInfo, &piProcInfo);
//Wait for your application to end
WaitForSingleObject(piProcInfo.hProcess,INFINITE);
...
HTH
Michel
--
----------------------------------------
Michel Leunen
www.leunen.com/
----------------------------------------
 

Re:How to run a Command in a progam and wait until finish?

Michel Leunen wrote:
Quote
CreateProcess(NULL, "yourApp.exe",
NULL, NULL, 0, 0, NULL, NULL,
&siStartInfo, &piProcInfo);

//Wait for your application to end
WaitForSingleObject(piProcInfo.hProcess,INFINITE);
CloseHandle( piProcInfo.hProcess );
CloseHandle( piProcInfo.hThread );
Yes, that is the prefered method.
Also available are the C spawn functions spawnl, spawnp, etc.
spawnl( P_WAIT, filename, filename)
 

{smallsort}