ExitProcess

Terminates the calling process and all threads within it.

Function Prototype

VOID ExitProcess(UINT uExitCode);

Parameters

Name Description
uExitCode The exit code for the calling process. Applications define their own exit codes, but standard conventions are often used. For example, 0 typically indicates success, and non-zero values indicate an error.

Remarks

The ExitProcess function terminates the calling process and all of its threads. The calling process is terminated.

If the process is part of a process group, all processes in the group are terminated.

The system does not perform the cleanup operations that occur when a process terminates normally. For example, the system does not call the exception handlers for the process.

The exit code returned by ExitProcess is the exit code of the process.

ExitProcess is used to terminate a process gracefully. Other functions like TerminateProcess forcibly terminate a process and should be used with caution.

Note: When ExitProcess is called, the system does not execute global destructors or C++ dynamic destructors. If you need to perform these cleanup operations, you should use the ExitThread function to terminate the current thread, or ensure that your program exits normally by returning from the main or WinMain function.

Example


#include <windows.h>
#include <iostream>

int main() {
    std::cout << "Starting process..." << std::endl;

    // Simulate some work
    // ...

    // Exit the process with an exit code of 0 (success)
    std::cout << "Exiting process successfully." << std::endl;
    ExitProcess(0);

    // This line will never be reached
    std::cout << "This will not be printed." << std::endl;

    return 0; // This return statement is also unreachable
}
            

Requirements

Attribute Value
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Header winuser.h (include Windows.h)
Library User32.lib
DLL User32.dll