Search code examples
cwindowscross-platformexit-codec-standard-library

Is there a Windows equivalent of standard(ish) UN*X process exit codes?


I need to decide which exit code to exit() with in different scenarios. I just read

Are there any standard exit status codes in Linux?

but I need to write something that is (sort of) cross-platform Linux+Windows. MS Windows does not seem to have something like /usr/include/sysexits.h; it only has C89's stdlib.h, which provides

#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1

So are these two the only thing that's portable? Or does Windows have some more elaborate platform-standard exit codes?

Note: I don't mean the System Error Codes of course.


Solution

  • There are a few exit codes that you often see on Windows if a process doesn’t terminate normally. These are large negative numbers like -1073741510, and are defined in ntstatus.h. The most common ones are:

    0xC0000005STATUS_ACCESS_VIOLATION: basically the equivalent of a segfault
    0xC0000409STATUS_STACK_BUFFER_OVERRUN
    0xC000013ASTATUS_CONTROL_C_EXIT: application was interrupted with Ctrl+C

    There is a large table on MSDN although I don’t think you will see most of them as exit codes.

    For application defined codes, the story is the same as on Linux. Usually you pick some exit codes and describe them in the documentation.

    For those writing batch files, be aware that CMD interprets the exit codes above as negative signed integers, so the test IF ERRORLEVEL 0 will succeed with these codes. So use the more strict equality test IF %ERRORLEVEL% NEQ 0.