How do I start another process and wait for it to complete?

The trick here is to realize that you can wait on the handle to a process. You need to use CreateProcess or ShellExecuteEx, because you need some way to get that handle to the newly-started process. Then you can use WaitForSingleObject to wait on the handle. The example code below uses ShellExecuteEx:

 
SHELLEXECUTEINFO sei = {0};

sei.cbSize = sizeof (SHELLEXECUTEINFO);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.lpVerb = "open";
sei.lpFile = "notepad.exe";
sei.nShow = SW_SHOWNORMAL;

if (ShellExecuteEx (&sei))
{
   WaitForSingleObject (sei.hProcess, INFINITE);
}

Some problems can arise with this technique. The most serious one is described in Tip 34, but you can also run into problems with processes which themselves start other processes, The commonest example being InstallShield (I hate InstallShield with such venom it's difficult to maintain my composure, so I'll just go have a beer instead...).

Download