Redirect all stdout/stderr to console

If your Windows executable is using /SUBSYSTEM:WINDOWS rather than /SUBSYSTEM:CONSOLE, something you will have noticed your output messages to stdout (printf, std::cout etc) will be missing.

You can easily solve this however by spawning your own Windows console window and redirecting stdout to your new console.

The code below will redirect all stdout & stderr messages to a new console window:

if (AllocConsole())
{
	AttachConsole(GetCurrentProcessId());
	HANDLE hConOut
		= CreateFileA("CONOUT$", GENERIC_WRITE, 
			FILE_SHARE_WRITE, nullptr, OPEN_EXISTING, 0, nullptr);
		
	SetStdHandle(STD_OUTPUT_HANDLE, hConOut);
	SetStdHandle(STD_ERROR_HANDLE, hConOut);

	//Ensures FILE* object remapped to new console window
	freopen("CONOUT$", "w", stdout);
	freopen("CONOUT$", "w", stderr);

	//Allows use of _write(1,...) & _write(2,...) to be redirected
	freopen("CONOUT$", "w", _fdopen(1, "w"));
	freopen("CONOUT$", "w", _fdopen(2, "w"));
}

3 responses to “Redirect all stdout/stderr to console”

  1. Heard some buzz about kubet11tax. Jumped on to take a look and the site loads fast, which is a massive plus. Could use a little design touch ups, but overall not bad! Give it a go: kubet11tax

  2. Casinolebull, eh? I’ve seen worse. The games selection is alright and the sign up bonus is relatively decent. Worth a punt if you’re bored. Find it here: casinolebull

  3. Bet364games… not sure if it’s connected to the bigger brand, but it does what it says on the tin. Games are alright, not amazing, but worth checking out if you’re after some quick spins. Check it here: bet364games

Leave a Reply

Your email address will not be published. Required fields are marked *