views:

993

answers:

4

I have an MFC legacy app that I help to maintain. I'm not quite sure how to identify the version of MFC and I don't think it would make a difference anyway.

The app can take some parameters on the command line; I would like to be able to set an errorlevel on exiting the app to allow a bat/cmd file to check for failure and respond appropriately.

I don't believe that exit() would work (hadn't tried it yet to be honest) because of the fact that this is an MFC app. Anyone know how to set the errorlevel returned by an MFC app? Can I just use exit()?

+1  A: 

There are a couple of solutions listed here I can't comment on which is better, but I'm sure at least one of them will get you there.

Harper Shelby
Thanks. That's what I was looking for.
Onorio Catenacci
+2  A: 

I can't take credit for this so please don't up this reply.

CWinApp::ExitInstance(); return myExitCode;

This will return the errorlevel to the calling batch file for you to then evaluate and act upon.

Lazarus
Why would I not vote your reply up? It's a helpful answer. No need to be so selfless. :-)
Onorio Catenacci
A: 

I've always had success returning the error code from CWinApp::ExitInstance. The default implementation, I believe, will return the value from the quit message. So you can use AfxPostQuitMessage if that is easier. But I have not personally tested the second method.

A: 

If your application refuses to update ERRORLEVEL in the DOS shell you run it from no matter what you do in the code of your program, it might help to run your app with "start /wait" so that the shell is locked until your program ends.