将 std::cout 重定向到新创建的控制台
当您在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想要使用 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
据我所知,您无法将标准句柄重定向到新控制台。您必须致电 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.