将 std::cout 重定向到新创建的控制台

发布于 2024-08-12 10:12:57 字数 183 浏览 2 评论 0原文

当您在 Windows 下创建 C++ 控制台应用程序时,您会自动获得为您创建的控制台窗口,并将 std::cout 输出到控制台窗口。

我有一个 GUI 应用程序,我还想为其创建一个控制台窗口。我可以使用 AllocConsole() 函数创建控制台窗口,但如何将 std::cout 重定向/附加到控制台以便输出显示在控制台窗口中?

When you create a C++ console application under Windows you automatically get the console window created for you and std::cout outputs to the console window.

I have a GUI application for which I also want to create a console window. I can create the console window using the AllocConsole() function, but how do I redirect / attach std::cout to the console so that the output appears in the console window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-08-19 10:12:57

您想要使用 GetStdHandle 和 SetStdHandle。鉴于我已经很久很久没有做过类似的事情了,你最好看看一些 一些示例

还有这个重复的问题

You want to use the GetStdHandle and SetStdHandle. Given that it is a long, long time since I have done anything similar, you would be better looking at some Some examples

There is also this duplicate question

花落人断肠 2024-08-19 10:12:57

据我所知,您无法将标准句柄重定向到新控制台。您必须致电 GetStdHandle(DWORD) 获取要写入的每个设备的句柄。使用此句柄,您需要使用适当的句柄调用 WriteFile、ReadFile、WriteConsole 和 ReadConsoleInput 函数来来回传递数据。

As far as I know you can't redirect the standard handles to the new console. You'll have to call GetStdHandle(DWORD) to get a handle for each device you want to write to. Using this handle you'll need to call the WriteFile, ReadFile, WriteConsole and ReadConsoleInput functions with the appropriate handle to pass data back and forth.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文