#10 – 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.

@echo off

MyProgram

IF "%ERRORLEVEL%" == "0" goto OK

:NotGood
    echo Bad news.  Program returned %ERRORLEVEL%
    goto End

:OK
    echo Everything A-OK

:End

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

4 Responses to #10 – The Return Value from Main() Sets ERRORLEVEL Variable in Windows

  1. S.K.C says:

    i didnt get it…how it works… ???

  2. Sean says:

    The ERRORLEVEL environment variable is automatically set by Windows to contain the return value of your program. In the example above, if your program returned a value of -1, the script would echo “Bad news. Program returned -1″. If your program returned 0, the script would echo “Everything A-OK”.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers