I'm trying to redirect traces on an output console on Windows Visual 2012, Linker/Subsystem = Windows (/SUBSYSTEM:WINDOWS), using a classical RedirectIOToConsole function.
Performing std::endl before AllocConsole seems to causes problems to display traces.
Below is my test:
#include <windows.h>
#include <stdio.h>
#include <iostream>
void RedirectIOToConsole()
{
FILE *conin, *conout;
AllocConsole();
freopen_s(&conin, "conin$", "r", stdin);
freopen_s(&conout, "conout$", "w", stdout);
freopen_s(&conout, "conout$", "w", stderr);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
// std::cout << "My Trace 1"; // Uncomment this line for Test 1
// std::cout << "My Trace 1" << std::endl; // Uncomment this line for Test 2
RedirectIOToConsole();
printf( "redirected console\n");
std::cout << "My Trace 2" << std::endl;
ch = getchar();
return 0;
}
- Run it as it is - output in Console window:
redirected console
My Trace 2 -> OK
- Uncomment line for test 1 - output in Console window:
redirected console
My Trace 2 -> OK
- Uncomment line for test 2 - output in Console window:
redirected console -> NOK