Redirecting Standard I/O to Windows Console

Warning! Some information on this page is older than 3 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

Nov 2009

Redirecting Standard I/O to Windows Console

Every process, no matter if under Windows or Linux, has standard text streams called input, output and error. Of course it makes most sense when you deal with command line applications, where you can directly interact with it using these streams. If you use Linux, you are probably familiar with redirecting streams to file using > operator or to another application using | operator. We use these streams in C++ code with standard C library (like printf and scanf functions) or standard C++ library (like std::cout and std::cin streams). But what if we write a GUI application in Windows (like a game), which has WinMain instead of main entry point and thus doesn't have console?

There is a simple WinAPI function called AllocConsole, which opens this black system console for your Win32 application. But today I've noticed that the console opened this way doesn't handle standard I/O! I've tried to do std::cout << "Hello World" and nothing happened. So after some googling I've found a hacky solution (or maybe rather a smart code snippet? :) in an article hosted at Andrew Tucker's Home Page, published in 1997 in Windows Developer Journal: Adding Console I/O to a Win32 GUI App. Here is my version of this code with all necessary includes:

#include <windows.h>
#include <ios>
#include <cstdio>
#include <io.h>
#include <fcntl.h>

void RedirectStandardIo()
  /* This clever code have been found at:
  Adding Console I/O to a Win32 GUI App
  Windows Developer Journal, December 1997
  Andrew Tucker's Home Page */

  // redirect unbuffered STDOUT to the console
  long lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
  int hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  FILE *fp = _fdopen(hConHandle, "w");
  *stdout = *fp;
  setvbuf(stdout, NULL, _IONBF, 0);

  // redirect unbuffered STDIN to the console
  lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
  hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  fp = _fdopen( hConHandle, "r" );
  *stdin = *fp;
  setvbuf(stdin, NULL, _IONBF, 0);

  // redirect unbuffered STDERR to the console
  lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
  hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
  fp = _fdopen( hConHandle, "w" );
  *stderr = *fp;
  setvbuf(stderr, NULL, _IONBF, 0);

  // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well

Comments (1) | Tags: winapi | Author: Adam Sawicki | Share


2017-02-22 09:47:24
of the old story. However, when a long time did not remember I was shocked to hear the passage of time to do so is ruthless, hurried between, chaotic fleeting, for the face, scattered old year.A woman with a temperature of not only move others, also touched yourself, just, and aerial life, everyone has his own Xibei cold, you were always a weak woman, also has its own anguish worries, you also need understand the need to release, we needed the space. So tired, I allow you to withdraw the dispute, but must be able to protect themselves, but also allows you to temporarily go into hiding, but it must be spring, I think, the heart if relieved to well as ever, and only put down the burden, all the way Ting Feng faint, will go easier.You only live once, you can not live too tired, should be wonderful, live smart, live happy! Friendship is the wine, the more the more concentrated wine, love is a flower, the more beautiful the more care. Ye friendship, love worth mentioning, when own, cherish after losing, be yourself, but indulging in grief, but in the memories of the past, a person must be a new beginning, do not let the sadness mill happy factor, do not let the past you plug in sorrow hall. Do not say you love who you are, life is long, can not predict tomorrow, good terms, everyone has their own helplessness. Not live to miss yesterday, but to wait for hope.Girl, your tears are precious, so do not just cry, your training is very important, so it should not foul language, your youth is very short, so it should not decadent. Girl, you learn to be alone in the deserted corner can also be silent joy, girl, you learn to be strong, in the season AnShang cross-flow, also can Angtou sunny, girl, you learn to smile, time to give yourself a smiley face , not only can regulate mood, but also a way to treat yourself. Girl, although many times no one can understand your grief, but still believe that a warm, beautiful, trust in the presence of these words, even if you remember, the only way to learn distressed themselves in order to make a good wife, good to live.Eyes were tears become increasingly clear, heart pain due to become even more transparent, because life experiences become even more abundance. Cried, the pain had gone through, give yourself a sunny day, it should be understood, to a

Post comment

Nick *
Your name or nickname
Your contact information (optional, will not be shown)
Text *
Content of your comment
Calculate *
(* - required field)
STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2017 Adam Sawicki
Copyright © 2004-2017 Adam Sawicki