开发者

Redirect std::cout to newly created console [duplicate]

开发者 https://www.devze.com 2022-12-12 03:58 出处:网络
This question already has answers here: 开发者_StackOverflow社区 Redirecting cout to a console in windows
This question already has answers here: 开发者_StackOverflow社区 Redirecting cout to a console in windows (11 answers) Closed 8 years ago.

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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消