问题描述
我的应用程序是一个GUI应用程序,通过终端(通过cout)有帮助(虽然可选)信息。
My application is a GUI app that has helpful (though optional) information through the terminal (via cout).
在Windows中,编译为控制台应用程序,或动态分配它)或者我不这样做。
In Windows I either have a console appear (by compiling as a console app, or allocating it dynamically) or I don't.
我的意图是利用控制台IF从控制台运行,但是完全忽略控制台,如果不是。 (基本上在Linux和OS X中会发生什么)。
My intention is to make use of the console IF it is being run from the console, but ignore the console completely if it was not. (Essentially what happens in Linux and OS X).
我不想重定向到一个文件(在使用cin的情况下,这不是一个可行的解决方案
I do not wish to redirect to a file (and in the case of using cin, this is not a viable solution anyway).
有没有办法将Windows中的GUI应用程序附加到从它运行的控制台,当且仅当它从控制台运行时? p>
Is there a way to attach a GUI app in Windows to the console it is run from, if and only if it is run from a console?
推荐答案
这是你的问题中的杀手细节。它在纸上很简单,只是首先调用AttachConsole(ATTACH_PARENT_PROCESS)尝试附加到现有的控制台。当您的程序从GUI程序(如Explorer或桌面快捷方式)启动时,这将失败。所以如果它返回FALSE,然后调用AllocConsole()创建自己的控制台。
This is the killer detail in your question. It is simple on paper, just first call AttachConsole(ATTACH_PARENT_PROCESS) to try to attach to an existing console. That will fail when your program got started from a GUI program like Explorer or a desktop shortcut. So if it returns FALSE then call AllocConsole() to create your own console.
使用 cin 命令处理器注意你的EXE,并检查它是控制台模式应用程序还是GUI应用程序。它会检测您的案例中的GUI应用程序,然后不会等待该过程完成。它再次显示提示,并等待输入。然后你会等待输入,但你会输,命令处理器先到达。您的输出也与命令提示符混合,很容易解决问题。
Using cin is a problem however. The command processor pays attention to your EXE and checks if it is console mode app or a GUI app. It will detect a GUI app in your case and then doesn't wait for the process to complete. It displays the prompt again and waits for input. You will then also wait for input but you'll lose, the command processor got there first. Your output is also intermingled with the command prompt, the easy problem to solve.
有一个简单的解决方法,您的用户应该启动您的程序与 start / wait yourapp
告诉命令处理器等待进程完成。问题是:没有人使用它。并且用户不会意识到,当他们键入输入,意图进入你的程序,但它实际上是由命令处理器解释发生了什么。生成一个神秘的错误消息或格式化硬盘驱动器。
There's a simple workaround for that, your user should start your program with start /wait yourapp
to tell the command processor to wait for the process to complete. Problem is: nobody ever uses that. And the user will not realize what happens when they type input, intending it to go into your program but it is actually interpreted by the command processor. Producing a mystifying error message or formatting the hard drive.
只有两个好的方法来解决这个无法解决的问题。或者将程序构建为控制台模式应用程序,并在您发现要显示GUI时调用FreeConsole()。或者总是调用AllocConsole()。这些不是很好的选择。第一种方法是由Windows上的Java JVM使用的方法。一个最古老的bug提交了JVM,并驱动Java程序员从闪烁的控制台窗口完全batty。
Only two good ways to solve this unsolvable problem. Either build your program as a console mode app and call FreeConsole() when you find out you want to display a GUI. Or always call AllocConsole(). These are not great alternatives. The first approach is the one used by the Java JVM on Windows. One of the oldest bugs filed against the JVM and driving Java programmers completely batty from the flashing console window.
第三个选择是唯一的一个,而你不想要的,创建另一个EXE将总是使用控制台。像Java一样,javaw.exe对java.exe。
The third alternative is the only decent one, and the one you don't want, create another EXE that will always use the console. Like Java does, javaw.exe vs java.exe.
一个诡计是可能的,您可以将该文件从yourapp2.exe重命名为yourapp.com。当用户在命令行提示符下键入yourapp时,首先选择它,桌面快捷方式仍然可以指向yourapp.exe。 Visual Studio使用这个技巧,devenv.com vs devenv.exe。
A trick is possible, you can rename that file from "yourapp2.exe" to "yourapp.com". It will be picked first when the user types "yourapp" at the command line prompt, a desktop shortcut can still point to "yourapp.exe". Visual Studio uses this trick, devenv.com vs devenv.exe.
这篇关于在Windows中的GUI应用程序中使用控制台,只有当它从控制台运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!