?
Solved

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

Posted on 2006-05-23
4
Medium Priority
?
3,511 Views
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

etc....

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);
      }
      else
      {
            return (CWinApp::ExitInstance());
      }

}


0
Comment
Question by:mromeo
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16748739
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:

CWinApp::ExitInstance();
return myExitCode;
0
 

Author Comment

by:mromeo
ID: 16750644
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?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 2000 total points
ID: 16750741
I tested this manually in cmd window, result is 0. But in batch file it shows right value:

int CtestApp::ExitInstance()
{
   CWinApp::ExitInstance();

   return 10;
}

Batch file:

test.exe
echo %errorlevel%
pause

Result:

C:\Tmp\test\Debug>test

C:\Tmp\test\Debug>echo 10
10

C:\Tmp\test\Debug>pause
Press any key to continue . . .
0
 

Author Comment

by:mromeo
ID: 16750767
Ok, so my code is correct. I will try this in a bat file.  Tahnks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

621 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