Article #1549: Windows how-to: create a stdout/stderr console at runtime for /subsystem:windows app

GitHub FLTK Project    FLTK News RSS Feed   
  FLTK Apps      FLTK Library      Forums      Links     Login 
 Home  |  Articles & FAQs  |  Bugs & Features  |  Documentation  |  Download  |  Screenshots  ]

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()
#include <stdio.h>
#include <stdlib.h>

#include <FL/Fl.H>
#include <FL/Fl_Window.H>

int main()
    // Open a DOS style console, redirect stdout to it
    AllocConsole() ;
    AttachConsole(GetCurrentProcessId()) ;
    freopen("CON", "w", stdout) ;
    freopen("CON", "w", stderr) ;

    printf("Hello world on stdout!\n");
    printf("Hello world on stderr!\n");
    Fl_Window win(400,400);;
    return Fl::run();
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 ]


Comments are owned by the poster. All other content is copyright 1998-2019 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to ''.