That was the clue I was looking for.
Using AllocConsole() the following code functions:
m_nCRTOut = _open_osfhandle((intptr_t) handle, _O_TEXT);
if (-1 == m_nCRTOut) return;
m_fpCRTOut = _fdopen(m_nCRTOut, "w");
if (! m_fpCRTOut) return;
setvbuf(m_fpCRTOut, 0, _IONBF, 0);
m_fOldStdOut = *stdout;
*stdout = *m_fpCRTOut;
However the above code doesn't function with
With AttachConsole(ATTACH_PARENT_PROCESS) (and AllocConsole) the
following code functions:
m_oldCout = std::cout.rdbuf();
Tor Lillqvist wrote:
>> I´ve been searching for this to attach the std c++ streams to the parent
>> console but so far without luck. If anyone knows how to, please...
> AttachConsole(ATTACH_PARENT_PROCESS). After that, re-open the streams
> to/from pseudo-filenames "CONOUT$" and "CONIN$".
> (AttachConsole is present only on XP or later, so you need to define
> _WIN32_WINNT appropriately to pick up its declaration from
> <windows.h>. And if you need your compiled code to run also on Windows
> 2000 or even earlier, you need to look up AttachConsole() dynamically
> at run-time, of course.)
> (We went through this same issue just some weeks ago on this very list.)