Table of Contents
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_LIMITED_INFORMATIONaccess right. For more information, see Process Security and Access Rights.
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
TerminateProcess() sends the value of the exit code to the process
in the second argument. The process terminates immediately using that code.
0in the second arg with any valid unsigned
int, such as 35.
Print the PID and the expected exit code.
Sent exit code '35' to PID 22640
GetExitCodeProcess()to get the exit code that the process returned.
Print the exit code from
Sent exit code '35' to PID 22568. PID 22568 returned exit code '35'.