How do you return an exit code from an MFC application?
Posted on 2006-05-23
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:
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:
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!
if (__argc > 1 && (strcmp(__argv, "-r") == 0 ))
// check to see if the service is running
BOOL running = IsServiceRunning(GetServiceName()); // calls QueryServiceStatus, etc.