Wait for a Windows Application to Close

Normally, when you launch a Windows process from another one, the call to create or launch that process returns immediately. Sometimes you need to wait for that child process or application to finish before continuing in the parent that launched it. This is often the case with installation programs that need to launch other installers.

The waiting in this code is accomplished by looping until the call to GetExitCodeProcess returns a value that indicates the child process is no longer active.

PROCESS_INFORMATION procInfo;
STARTUPINFO startupInfo;
GetStartupInfo(&startupInfo);

TCHAR fileName[MAX_PATH];
GetModuleFileName(0, fileName, sizeof(fileName)/sizeof(TCHAR));
_tcscpy_s((TCHAR *)(_tcsrchr(fileName, _T('\\')) + 1), MAX_PATH, _T("childapp.exe"));

CreateProcess(0, fileName, 0, 0, FALSE, 0, 0, 0, &startupInfo, &procInfo);

DWORD termStatus;
do
{
   GetExitCodeProcess(procInfo.hProcess, &termStatus);
   Sleep(1000);
}while(termStatus == STILL_ACTIVE);

(TCHAR and _tcs functions are macros for MBCS/Unicode compatibility)