How do you return an exit code from an MFC application?

Posted on 2006-05-23
Last Modified: 2013-11-20
I have an MFC application which is a Windows service.  If this application is run on the command line with a certain flag (-r), instead of starting the service, it merely tests to see if this service is running and then returns 1 or 0, depending on the result.  For example:

MyService -r

returns 0 if MyService is not running and 1 if it is running

Now, I want to be able to write a bat file to capture the exit code and say something like:

MyService -r
if %errorlevel% 1 then goto running
if %errorlevel% 0 then goto notrunning


The problem is, the MFC app always returns 0, regardless if it is running or not.  I have overridden ExitInstance to return the value and when I log that value, it is correect. But when the app gets back to the Windows shell, the errorlevel value is always 0.  Here is my ExitInstance code.  Is there something else that I need to override to get the correct exit code to back to the DOS shell?  Thanks!

BOOL CMainApp::ExitInstance()
      if (__argc > 1 && (strcmp(__argv[1], "-r") == 0 ))
            // check to see if the service is running
            BOOL running = IsServiceRunning(GetServiceName());   // calls QueryServiceStatus, etc.
            return (running);
            return (CWinApp::ExitInstance());


Question by:mromeo
    LVL 48

    Expert Comment

    Exit code of MFC application is value returned by ExitInstance. You can return any int value from ExitInstance. BTW, it is defined as virtual int ExitInstance.
    If you want to call CWinApp::ExitInstance and return your own value, you can write:

    return myExitCode;

    Author Comment

    I am doing that. But yet, when I type "echo %errorlevel%" at a command prompt right after my program exits, it always returns 0....regardless of the vaule returned by ExitInstance.  I know my return value from ExitInstance is correct because I was logging it, and it definitely was NOT zero.  Any ideas??  Could this be an ms-dos issue?
    LVL 48

    Accepted Solution

    I tested this manually in cmd window, result is 0. But in batch file it shows right value:

    int CtestApp::ExitInstance()

       return 10;

    Batch file:

    echo %errorlevel%



    C:\Tmp\test\Debug>echo 10

    Press any key to continue . . .

    Author Comment

    Ok, so my code is correct. I will try this in a bat file.  Tahnks.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now