7.2. Getting the Exit Code

We don’t want to terminate a process forcefully most of the time. We want the process to exit gracefully. The exit code for a child process provides vital information about what the process is doing. It gives us a chance to wait for the process to exit instead of terminating it. This section explains how to use GetExitCodeProcess() to retrieve the exit code.


Retrieves the termination status of the specified process.

BOOL GetExitCodeProcess(
    HANDLE  process_handle, // A handle to the process.
    DWORD exit_code         // A pointer to the exit code of the process.


GetExitCodeProcess() requires additional flags in the process handle.

The handle must have the PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION access right. For more information, see Process Security and Access Rights.

7.2.1. Terminate with an Exit Code

Copy file lab7.1.c to lab7.2.c for this exercise.

Typically, we use the return of return 0 or return 1 to exit our application to indicate if the process exited in a state of success or failure. TerminateProcess() sends the value of the exit code to the process in the second argument. The process terminates immediately using that code.

TerminateProcess(hProcess, uExitCode);
  1. Replace the 0 in the second arg with any valid unsigned int, such as 35.

  2. Print the PID and the expected exit code.

Expected Output

Sent exit code '35' to PID 22640

7.2.2. Get the Exit Code

  1. Add the PROCESS_QUERY_INFORMATION flag to OpenProcess().

  2. Use GetExitCodeProcess() to get the exit code that the process returned.

  3. Print the exit code from GetExitCodeProcess().

Expected Output

Sent exit code '35' to PID 22568.
PID 22568 returned exit code '35'.