简体   繁体   English

从Windows App在命令提示符上显示错误字符串或代码

[英]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. 我正在一个VC ++ Windows应用程序上工作,该应用程序支持GUI以及基于非GUI的执行。 The non GUI based execution is designed for scripting purpose. 非基于GUI的执行设计用于脚本编写目的。 Now, if I executed that application in GUI mode I can show message box for user acknowledgement in case of error or exception. 现在,如果我以GUI模式执行该应用程序,则可以在出现错误或异常的情况下显示消息框供用户确认。 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. 我尝试了AllocConsole(...),但它弹出了另一个窗口。 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(...). 在此错误消息应该显示在同一提示上,而不是从AllocConsole(...)弹出的控制台上显示。

Any help or idea? 有什么帮助或想法吗?

Thanks, Omky 谢谢,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). 这不是控制台应用程序,因此,在控制台运行时,它会为Windows应用程序生成另一个进程,该进程不会将其std映射到控制台,因为控制台会在启动应用程序后立即返回(即,它不会等待它启动)。终止)。

In this case you could do the following (kernel32): 在这种情况下,您可以执行以下操作(kernel32):

try 尝试

AttachConsole(-1); 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(...); 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. 如果您连接到控制台,这看起来会很奇怪,因为您将能够在控制台中输入文字并与之交互以在其中运行其他内容,而您的应用将向其发送文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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