简体   繁体   中英

Displaying the error string or code on Command Prompt from Windows App

I am working on a VC++ windows application which support GUI as well as non-GUI based execution. The non GUI based execution is designed for scripting purpose. Now, if I executed that application in GUI mode I can show message box for user acknowledgement in case of error or exception. However, If the application is executed in silent mode then I want to show some error message on command prompt, how to do this?. I have tried AllocConsole(...) but it pops up another window. I want to avoid this. My use case is below -

CMD> myapplication.exe -silent
CMD> Error XXXX: Application message.

Here the error message should be displayed on same prompt not on popped up console from AllocConsole(...).

Any help or idea?

Thanks, Omky

This is not a console application, therefore when the console runs it spawns another process for the windows app which doesnt get its std out mapped to the console, since the console returns as soon as the app is launched (ie. it doesnt wait it to terminate).

In this case you could do the following (kernel32):

try

AttachConsole(-1);

if you get it then its fine

if not it means you were not run from a console, and you need to use

AllocConsole(...);

If you attach to the console this will look weird tough, since you will be able to type and interact with the console to run other stuff in it, while you app will be sending text to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM