[ Return to Articles | Show Comments | Submit Comment ]
Article #1549: Windows how-to: create a stdout/stderr console at runtime for /subsystem:windows app
Created at 11:57 Sep 19, 2017 by greg.ercolano
Last modified at 11:59 Sep 19, 2017
If you have an FLTK Windows GUI application (built with /subsystem:windows), you can create a DOS style window and redirect stdout/stderr to it at runtime.
This shows a pretty simple way to do this using AllocConsole() and AttachConsole() which can give a Windows application (that backrgrounds itself) a way to display stdout/stderr.
#define _WIN32_WINNT 0x0501 // needed for AttachConsole
#include <windows.h> // AllocConsole()
#include <Wincon.h> // AttachConsole()
// Open a DOS style console, redirect stdout to it
freopen("CON", "w", stdout) ;
freopen("CON", "w", stderr) ;
printf("Hello world on stdout!\n");
printf("Hello world on stderr!\n");
[ Listing ]
[ Submit Comment ]
From greg.ercolano, 12:04 Sep 19, 2017 (score=3)
..and yes, you /could/ also compile your app with /SUBSYSTEM:CONSOLE to get similar results: this lets you see stdout/stderr in the DOS window you invoked the program from, or if you clicked on the app to run it, that first opens a separate DOS window then runs your app inside it.
However, in THIS example shown here, it shows a way to /conditionally/ open a DOS style window for redirecting stdout/err in a /SUBSYSTEM:WINDOWS app, where the application runs in the background when invoked from a DOS terminal, which can be useful too. For instance, you could make the DOS style window only appear when needed, such as to display some debug info, then you can later make it go away with FreeConsole().
[ Reply ]