C++ – Redirect std::cout to newly created console

cconsolecoutwindows

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?

Best Answer

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