How do I call ::CreateProcess in c++ to launch a Windows executable?

jm. picture jm. · Sep 3, 2008 · Viewed 154k times · Source

Looking for an example that:

  1. Launches an EXE
  2. Waits for the EXE to finish.
  3. Properly closes all the handles when the executable finishes.

Answer

1800 INFORMATION picture 1800 INFORMATION · Sep 3, 2008

Something like this:

STARTUPINFO info={sizeof(info)};
PROCESS_INFORMATION processInfo;
if (CreateProcess(path, cmd, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
    WaitForSingleObject(processInfo.hProcess, INFINITE);
    CloseHandle(processInfo.hProcess);
    CloseHandle(processInfo.hThread);
}