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 10:42 Jun 05, 2020

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) ;

    fprintf(stdout, "Hello world on stdout!\n");
    fprintf(stderr, "Hello world on stderr!\n");
    Fl_Window win(400,400);
    win.show();
    return Fl::run();
}

Listing ]


Comments

Submit Comment ]

From AlbrechtS, 05:53 Jun 05, 2020 (score=3)

I'm wondering: shouldn't these two lines:

  printf("Hello world on stdout!\n");
  printf("Hello world on stderr!\n");
read:
  printf("Hello world on stdout!\n");
  fprintf(stderr, "Hello world on stderr!\n");
or even (for clarity):
  fprintf(stdout, "Hello world on stdout!\n");
  fprintf(stderr, "Hello world on stderr!\n");
?
Reply ]

From greg.ercolano, 07:37 Jun 05, 2020 (score=3)

Yep, the latter -- fixed!
Reply ]

From AlbrechtS, 10:12 Jun 05, 2020 (score=3)

Thanks for the quick fix.

One more quick note: cross comilation (mingw-w64) under Linux failed because of its case sensitivity: 'Wincon.h' has to be 'wincon.h' (all lowercase). It's usually not important, but anyway...

FTR: when running the program under Linux with `wine` it used the existing console window instead of creating a new one - which is kinda expected and likely due to the behavior of `wine`.
Reply ]

From greg.ercolano, 10:43 Jun 05, 2020 (score=3)

OK, fixed that too; Wincon.h -> wincon.h
Reply ]

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-2020 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.