创建一个进程并检查其退出代码

此示例启动记事本,等待它关闭,然后获取其退出代码。

#include <Windows.h>

int main()
{
   STARTUPINFOW si = { 0 };
   si.cb = sizeof(si);
   PROCESS_INFORMATION pi = { 0 };

   // Create the child process
   BOOL success = CreateProcessW(
      L"C:\\Windows\\system32\\notepad.exe",  // Path to executable
      NULL,                                   // Command line arguments
      NULL,                                   // Process attributes
      NULL,                                   // Thread attributes
      FALSE,                                  // Inherit handles
      0,                                      // Creation flags
      NULL,                                   // Environment
      NULL,                                   // Working directory
      &si,                                    // Startup info
      &pi);                                   // Process information

   if (success)
   {
      // Wait for the process to exit
      WaitForSingleObject(pi.hProcess, INFINITE);

      // Process has exited - check its exit code
      DWORD exitCode;
      GetExitCodeProcess(pi.hProcess, &exitCode);

      // At this point exitCode is set to the process' exit code

      // Handles must be closed when they are no longer needed
      CloseHandle(pi.hThread);
      CloseHandle(pi.hProcess);
   }
}

参考文献(MSDN):