Saturday, May 24, 2008

How do I wait for a process to terminate?

Suppose you want to execute a command line utility and wait for it to finish before continuing. Then you can construct the command line including the executable path at the beginning and call the following function.
int ExecAndWait(char *cmdline, char *workingdir)
{
STARTUPINFO startupinfo = {0};
PROCESS_INFORMATION procinfo = {0};

startupinfo.cb = sizeof(startupinfo);
if(CreateProcess(NULL, cmdline, NULL, NULL, FALSE,
CREATE_NO_WINDOW, NULL, workingdir,
&startupinfo, &procinfo) == 0)
return 0;

WaitForSingleObject(procinfo.hProcess, INFINITE);
return 1;
}

No comments: