The Return Value from Main() Sets ERRORLEVEL Variable in Windows

If your Main() function returns an integer value, you can check that value in the calling program or script using the ERRORLEVEL environment variable.  By convention, a return value of 0 indicates success and any other value indicates an error.  Below is a sample .bat file that calls a program called MyProgram and then checks the return value.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
@echo off
 
MyProgram
 
IF "%ERRORLEVEL%" == "0" goto OK
 
:NotGood
    echo Bad news.  Program returned %ERRORLEVEL%
    goto End
 
:OK
    echo Everything A-OK
 
:End