Visual C++ 启用控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2501968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:43:32  来源:igfitidea点击:

Visual C++ Enable Console

c++visual-c++console

提问by Attic

I created an Empty Project in Visual C++, but now I need the Console to display debug output.

我在 Visual C++ 中创建了一个空项目,但现在我需要控制台来显示调试输出。

How can I enable the Console without recreating the project or show the output in the VS output window?

如何在不重新创建项目或在 VS 输出窗口中显示输出的情况下启用控制台?

回答by Ryan Woodard

Here's some code you can insert to get a console window in a GUI'd windows app that starts in WinMain. There are other ways to accomplish this but this is the most compact snippet I've found.

这里有一些代码,您可以插入以在 WinMain 中启动的 GUI 窗口应用程序中获取控制台窗口。还有其他方法可以做到这一点,但这是我发现的最紧凑的片段。

//Alloc Console
//print some stuff to the console
//make sure to include #include "stdio.h"
//note, you must use the #include <iostream>/ using namespace std
//to use the iostream... #incldue "iostream.h" didn't seem to work
//in my VC 6
AllocConsole();
freopen("conin$","r",stdin);
freopen("conout$","w",stdout);
freopen("conout$","w",stderr);
printf("Debugging Window:\n");

回答by Reed Copsey

You can always call AllocConsolein code to create a console for your application, and attach it to the process. FreeConsolewill remove the console, detaching the process from it, as well.

您始终可以在代码中调用AllocConsole来为您的应用程序创建一个控制台,并将其附加到进程中。 FreeConsole将删除控制台,并从中分离进程。

If you want all standard output stream data to go to the console, you need to also use SetStdHandle to redirect the output appropriately. Here is a page showing working code to do this full process, including allocating the console and redirecting the output.

如果您希望所有标准输出流数据都转到控制台,您还需要使用 SetStdHandle 适当地重定向输出。这是一个页面,显示了执行此完整过程的工作代码,包括分配控制台和重定向输出。

回答by Dani

You can write to the vs output window with OutputDebugString. http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx

您可以使用 OutputDebugString 写入 vs 输出窗口。http://msdn.microsoft.com/en-us/library/aa363362(VS.85).aspx