Exit Codes Windows Info

In the seemingly sterile output of a command-line program—a lone integer returned to the operating system—lies a sophisticated, often misunderstood contract between a process and its caller. On Windows, this integer is the exit code (or "return code"), and while the convention 0 for success and non-zero for failure is universal, the depth beneath is uniquely shaped by Windows' architecture, its legacy subsystems, and the perils of cross-platform assumptions. 1. The Kernel's Handshake: How Exit Codes Really Work When a Windows process terminates—whether by returning from main() , calling ExitProcess() , or suffering an unhandled exception—the kernel records a 32-bit unsigned integer inside the EPROCESS block. This value persists until the process object is reaped by WaitForSingleObject() or GetExitCodeProcess() .

If your main() throws an uncaught C++ exception, the CRT catches it, calls terminate() , and then ExitProcess(3) . The code 3 means nothing about your logic—it simply signals "CRT abnormal termination."

These are STILL_ACTIVE (thread) and STATUS_PENDING (process). Seeing them from GetExitCodeProcess means you called it before the process actually exited. This is a classic race. exit codes windows

This overlap is a trap: an exit code of 2 could mean "invalid parameter" (application-defined), or it could mean ERROR_FILE_NOT_FOUND from a failed CreateFile . Without the program's documentation, you cannot disambiguate. Three common scenarios produce exit codes that are technically correct but semantically useless:

> err 0xC0000005 # for hex 0xc0000005 / decimal -1073741819 STATUS_ACCESS_VIOLATION Check against Win32 error codes: In the seemingly sterile output of a command-line

In cmd.exe , the exit code of a batch file is the exit code of the last command executed . A batch file that copies a file and then echoes a message will return 0 even if the copy failed, because echo always succeeds. This forces developers to use exit /b %errorlevel% explicitly.

Crucially, the exit code is the return value of main() in the C runtime sense. The CRT wraps main() , captures its return value, and passes it to ExitProcess() . If you never call ExitProcess explicitly, the CRT does it for you. The Kernel's Handshake: How Exit Codes Really Work

> net helpmsg 2 The system cannot find the file specified. If the program is well-known (e.g., robocopy , xcopy ), consult its documentation—they reuse Win32 error codes with different meanings.