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.

20:10
Sat
28
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
  http://dslweb.nwnexus.com/~ast/dload/guicon.htm
  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
  std::ios::sync_with_stdio();
}

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

Comments

YLQ
2017-02-22 09:47:24
of http://www.nikefree-run.net/ the http://www.rosherun.co.uk/ old http://www.oakleys-frame.net/ story. http://www.new-balance-schuhe.de/ However, http://raiders.nfljersey.us.com/ when http://www.coachoutlet-online.com.co/ a http://www.the-northface.com.co/ long http://www.burberryoutlet-sale.net/ time http://www.nike-roshe-run.de/ did http://www.supra-shoes.org/ not http://www.longchamp.com.de/ remember http://www.adidas.com.se/ I http://www.kates-spade.com/ was http://www.cheap-mlbjerseys.us.com/ shocked http://www.cheap-jordans.net/ to http://raptors.nba-jersey.com/ hear http://www.barbours.us.com/ the http://wizards.nba-jersey.com/ passage http://www.oakleys.in.net/ of http://www.cheap-baseballbats.us/ time http://www.mcmhandbags.com.co/ to http://www.monclers-outlet.us.com/ do http://www.dsquared2.us.com/ so http://bucks.nba-jersey.com/ is http://www.pandora.com.de/ ruthless, http://www.abercrombie-kid.us.com/ hurried http://www.rosheruns.us/ between, http://www.ed-hardy.us.com/ chaotic http://www.ugg-australia.com.de/ fleeting, http://www.oakleyoutlet.fr/ for http://www.chi-flatiron.us.com/ the http://www.nike-shoescanada.ca/ face, http://panthers.nfljersey.us.com/ scattered http://www.christianlouboutin.org.uk/ old http://michaelkors.misblackfriday.com/ year.A http://www.soccer-shoesoutlet.com/ woman http://www.abercrombie-and-fitch.us.com/ with http://coach-outlet.tumblr.com/ a http://www.raybanoutlet.ca/ temperature http://www.replica-handbags.net.co/ of http://www.christianlouboutinshoesoutlet.org/ not http://www.northfaceoutlet.com.co/ only http://www.mcmbackpacks.in.net/ move http://www.michael-kors-outlet.us.org/ others, http://www.airjordans.us/ also http://www.oakleys2016.com/ touched http://www.pradahandbags.net.co/ yourself, http://www.coach-outletonline.net.co/ just, http://www.michael-kors.net.co/ and http://www.uggs-boots.net.co/ aerial http://www.barbour.in.net/ life, http://nuggets.nba-jersey.com/ everyone http://www.basketballshoes.com.co/ has http://www.iphone-cases.in.net/ his http://www.nike-factorys.us/ own http://www.monclerjackets.in.net/ Xibei http://www.adidas-schuheonline.de/ cold, http://redskins.nfljersey.us.com/ you http://www.coach-factoryyoutletonline.net/ were http://www.ugg-boots-australia.com.au/ always http://www.thenorthface.com.de/ a http://seahawks.nfljersey.us.com/ weak http://www.rolexwatchesforsale.us.com/ woman, http://titans.nfljersey.us.com/ also http://www.true-religion.com.co/ has http://www.longchamp-bags.us.com/ its http://www.rayban-sunglasses.co/ own http://www.thomas-sabo.com.de/ anguish http://www.tommyhilfigerca.ca/ worries, http://www.timberland-boots.com.co/ you http://www.swarovskissale.co.uk/ also http://www.oakley-outletonline.com.co/ need http://www.oakleyoutlet.ar.com/ understand http://www.prada.com.de/ the http://www.rayban-pascher.fr/ need http://www.valentino-shoesoutlet.us/ to http://www.christian-louboutinshoes.in.net/ release, http://www.michaelkors-outletonline.cc/ we http://buccaneers.nfljersey.us.com/ needed http://clippers.nba-jersey.com/ the http://www.nike-airmax.us.com/ space. http://www.michaelkorsoutlet.ar.com/ So http://www.timberlandshoes.net.co/ tired, http://www.hermes-outlet.net.co/ I http://www.wedding-dresses.cc/ allow http://www.sunglasses-outlet.net/ you http://www.michael-korshandbags.us.org/ to http://www.ray-banssunglasses.org/ withdraw http://www.omegarelojes.es/ the http://www.nike-max.fr/ dispute, http://www.michaeljordan.com.de/ but http://www.nba-shoes.com/ must http://www.hollister-abercrombie.com.se/ be http://49ers.nfljersey.us.com/ able http://www.montblancpens-sale.com/ to http://www.cheap-rolex-watches.org.uk/ protect http://www.hermesbirkin-outlet.com/ themselves, http://www.burberry-outletcanada.ca/ but http://chargers.nfljersey.us.com/ also http://www.ray-bansoutlet.in.net/ allows http://www.polo-ralph-lauren.de/ you http://www.raybans-outlet.cc/ to http://www.newbalancecanada.ca/ temporarily http://www.ralphslaurenoutlet.us.com/ go http://www.cheap-raybansoutlet.in.net/ into http://www.nike-air-max.com.se/ hiding, http://www.nike-tnrequinpascher.fr/ but http://www.ray-bans.net.co/ it http://www.outlet-burberry.net.co/ must http://www.poloralphlaurenoutlet.net.co/ be http://www.pulseraspandora.com.es/ spring, http://www.lacosteoutlet.us.com/ I http://www.vans-schuhe.com.de/ think, http://www.hollister-clothing.in.net/ the http://www.newoutletonlinemall.com/ heart http://jaguars.nfljersey.us.com/ if http://www.uhren-shop.com.de/ relieved http://www.abercrombiefitchs.cc/ to http://www.adidasshoesca.ca/ well http://www.toms-shoes.net.co/ as http://www.giuseppezanotti.com.co/ ever, http://www.to-coachoutlet.com/ and http://www.rayban.com.de/ only http://www.uggsstore.us.com/ put http://jazz.nba-jersey.com/ down http://www.nikerosherun.us/ the http://www.tory-burchoutlet.com.co/ burden, http://www.michaelkors.com.se/ all http://www.barbour-jackets.us.com/ the http://hawks.nba-jersey.com/ way http://www.bottega-venetas.cc/ Ting http://lakers.nba-jersey.com/ Feng http://www.air-huaracheshoes.co.uk/ faint, http://www.michaelkors-bags.com.co/ will http://www.tomsoutlet-online.net/ go http://www.raybans-outlet.com.co/ easier.You http://www.bcbg-maxazria.ca/ only http://www.air-max.com.de/ live http://www.nike-rosherun.com.es/ once, http://www.soft-ballbats.com/ you http://www.juicycouture.com.co/ can http://www.hollisterclothingstore.org/ not http://www.ugg-boots.us.org/ live http://www.iphone-cases.net/ too http://www.timberlands-paschere.fr/ tired, http://www.rolex-watches.us.com/ should http://www.nike-outlet.us.org/ be http://www.hogan.com.de/ wonderful, http://www.toms-outlets.us.com/ live http://bears.nfljersey.us.com/ smart, http://www.marc-jacobs.us.com/ live http://www.cheap-jerseys.mex.com/ happy! http://www.oakleys.org.es/ Friendship http://www.versaceoutlet.us.com/ is http://www.oakleys-2017.in.net/ the http://www.oakleyssunglasses.in.net/ wine, http://www.longchamps.us.com/ the http://www.ralph-laurenpolosoutlet.com/ more http://www.mk-outletonline.us/ the http://www.givenchy.in.net/ more http://www.burberrys-outlets.org.uk/ concentrated http://www.nike-skor.com.se/ wine, http://www.katespades-outlet.in.net/ love http://www.airmax-2015.org/ is http://www.michael-kors.com.es/ a http://www.air-huarache.co.uk/ flower, http://www.nikeshoes.org.es/ the http://www.nike-air-max.com.de/ more http://www.tommy-hilfiger.in.net/ beautiful http://rayban.blackfriday.in.net/ the http://www.swarovski-australia.com.au/ more http://vikings.nfljersey.us.com/ care. http://www.nike-schoenen.co.nl/ Ye http://www.prada-shoes.com.co/ friendship, http://www.ray-bansoutlet.it/ love http://broncos.nfljersey.us.com/ worth http://www.uggs-boots.com.co/ mentioning, http://www.coach-factory.in.net/ when http://www.ralphs-laurens.co.uk/ own, http://www.tracksuits-store.com/ cherish http://www.nikestore.com.de/ after http://www.cheapmichaelkors.us.org/ losing, http://www.insanity-workout.us.com/ be http://www.cheap-michaelkors.com/ yourself, http://www.tory-burch.us.org/ but http://timberwolves.nba-jersey.com/ indulging http://warriors.nba-jersey.com/ in http://www.hollisteronlineshop.com.de/ grief, http://www.burberrys-bags.net.co/ but http://www.tomsshoes-outlet.us.com/ in http://www.bcbg-dresses.com/ the http://www.nike-mercurial.in.net/ memories http://www.nikestore.us/ of http://heat.nba-jersey.com/ the http://www.cheapuggs-boots.com.co/ past, http://bulls.nba-jersey.com/ a http://www.tory-burchsandals.in.net/ person http://www.vans-shoes.co.uk/ must http://www.truereligions.net/ be http://www.oakleysunglasses-canada.ca/ a http://www.longchampoutlet.com.co/ new http://www.bottega-veneta.in.net/ beginning, http://suns.nba-jersey.com/ do http://mavericks.nba-jersey.com/ not http://www.oakleysunglasses2017.com/ let http://www.ray-bans.co.uk/ the http://www.burberry-outletonline.cc/ sadness http://coach.misblackfriday.com/ mill http://www.michaelkors.so/ happy http://www.abercrombie-andfitch.ca/ factor, http://www.womenclothes.in.net/ do http://www.p90xworkout.in.net/ not http://www.michael-korssale.us.com/ let http://www.philipp-plein.us.com/ the http://www.converses-outlet.com/ past http://www.ok-em.com/ you http://magic.nba-jersey.com/ plug http://www.nike-schuhe.com.de/ in http://www.salvatoreferragamo.in.net/ sorrow http://coach.euro-us.net/ hall. http://www.nike-air-max.com.au/ Do http://www.rolexwatches-canada.ca/ not http://www.nikeair-max.ca/ say http://www.uggs.co.nl/ you http://www.jimmy-choos.com/ love http://www.underarmour.us.com/ who http://pelicans.nba-jersey.com/ you http://www.horlogesrolex.nl/ are, http://www.giuseppe-zanotti.net/ life http://www.pandorajewelry.top/ is http://www.abercrombiehollister.nl/ long, http://www.mcm-bags.us.org/ can http://www.beatsheadphone.in.net/ not http://www.hollisters-canada.ca/ predict http://www.cheapjerseys.net.co/ tomorrow, http://www.nikeair--max.fr/ good http://www.nfl-jersey.us.org/ terms, http://www.asics-shoesoutlet.com/ everyone http://airmax.misblackfriday.com/ has http://www.canadagooses-jackets.com/ their http://76ers.nba-jersey.com/ own http://www.ferragamo.com.co/ helplessness. http://www.tommy-hilfiger.co.nl/ Not http://cavaliers.nba-jersey.com/ live http://cowboys.nfljersey.us.com/ to http://www.babylisspros.in.net/ miss http://www.oakleys-outlet.in.net/ yesterday, http://www.guessfatory.net/ but http://www.mizuno-running.net/ to http://www.nike-air-force.de/ wait http://www.uggsoutlet.com.co/ for http://www.converse.com.de/ hope.Girl, http://www.canadagoosesonline.com/ your http://www.the-northfaces.net.co/ tears http://www.oakleys-outlet.it/ are http://www.oakleys-sunglasses.in.net/ precious, http://colts.nfljersey.us.com/ so http://www.juicycoutureoutlet.net.co/ do http://uggboots.misblackfriday.com/ not http://www.ugg-bootsclearance.com/ just http://www.christian-louboutins.in.net/ cry, http://www.mk-com.com/ your http://www.air-maxschoenen.nl/ training http://www.cheapreplica-watches.in.net/ is http://www.burberry-outletsonline.org.uk/ very http://www.montres-pascher.fr/ important, http://www.raybanssunglasses.in.net/ so http://www.converse.net.co/ it http://www.nikeair-max.es/ should http://www.vibram-fivefingers.in.net/ not http://www.reebok.com.de/ foul http://www.levisjeans.com.co/ language, http://www.mbt-shoes.us.com/ your http://www.timberlandbootsoutlet.us.com/ youth http://www.mcm-handbags.org/ is http://www.ferragamoshoes.in.net/ very http://www.cheap-michaelkors.in.net/ short, http://www.nikefree5.net/ so http://www.ghdhairstraightener.cc/ it http://www.uggboots.com.de/ should http://www.handbagsoutlet.in.net/ not http://www.adidassuper-star.de/ decadent. http://www.the-northfaces.us.com/ Girl, http://www.michaelkorsoutletonline.net.co/ you http://www.tommy-hilfiger-online.de/ learn http://www.christianlouboutinoutlet.net.co/ to http://knicks.nba-jersey.com/ be http://trailblazers.nba-jersey.com/ alone http://www.michaelkorsoutlet-online.ar.com/ in http://www.katespadeoutlet.gb.net/ the http://www.nikeairmaxnc.co.uk/ deserted http://pacers.nba-jersey.com/ corner http://saints.nfljersey.us.com/ can http://www.mcms-handbags.com/ also http://www.cheap-oakleyssunglasses.com/ be http://celine.blackfriday.in.net/ silent http://www.kate-spades.com.co/ joy, http://www.nike-free-run.de/ girl, http://www.coach-factoryoutlet.net.co/ you http://www.michael-kors-australia.com.au/ learn http://www.michaelkorsbags.us.org/ to http://www.nikeshoesoutlet.org.uk/ be http://www.nikeskoes.dk/ strong, http://bills.nfljersey.us.com/ in http://www.chiflatiron.net.co/ the http://hornets.nba-jersey.com/ season http://www.adidas-superstars.nl/ AnShang http://www.co-aol.com/ cross-flow, http://www.adidas.us.com/ also http://www.toms-shoesoutlet.us/ can http://texans.nfljersey.us.com/ Angtou http://www.nikeshoes-outlet.com/ sunny, http://www.tommy-hilfigers.com/ girl, http://www.jordanretro.org/ you http://rockets.nba-jersey.com/ learn http://www.michaelkors-canadaoutlet.ca/ to http://www.raybans-sunglass.com/ smile, http://ralphlauren.blackfriday.in.net/ time http://falcons.nfljersey.us.com/ to http://longchamp.blackfriday.in.net/ give http://www.jordan-shoes.com.co/ yourself http://kings.nba-jersey.com/ a http://www.rayban-pas-cher.fr/ smiley http://www.armani-exchange.in.net/ face http://www.oakley.com.de/ , http://www.celine-bags.org/ not http://www.calvin-kleins.net/ only http://www.raybans.org.es/ can http://www.burberry-handbagssale.com.co/ regulate http://www.cheap-jerseys.cc/ mood, http://www.beatsbydre.com.co/ but http://www.the-northfacejackets.net.co/ also http://chiefs.nfljersey.us.com/ a http://www.christianlouboutin-shoes.ca/ way http://www.true-religions.com/ to http://www.coachblackfriday.com/ treat http://www.scarpe-hoganoutlets.it/ yourself. http://packers.nfljersey.us.com/ Girl, http://www.cheapnhljerseys.us.com/ although http://www.cheap-rayban.com.co/ many http://www.hollisters.us.com/ times http://www.swarovskijewelrys.in.net/ no http://www.toryburchsale.com.co/ one http://www.ralphlauren-polos.com.co/ can http://www.ralph-laurenoutletonline.com/ understand http://www.burberryoutlet-sale.com.co/ your http://www.polos-ralphlauren.us.org/ grief, http://www.polos-outlets.com/ but http://www.uggs-online.com/ still http://coachoutlet.euro-us.net/ believe http://www.barbour-jacketsoutlet.net/ that http://www.salomon-schuhe.com.de/ a http://www.lauren-ralphsoutlet.co.uk/ warm, http://www.marc-jacobsonsale.com/ beautiful, http://www.toms--outlet.com.co/ trust http://www.zxcoachoutlet.com/ in http://michaelkors.euro-us.net/ the http://www.burberryoutlets.net.co/ presence http://www.michael-kors-taschen.com.de/ of http://www.thenorthfacejackets.fr/ these http://www.truereligion-outlet.us.org/ words, http://www.newbalance-shoes.org/ even http://www.poloralphlauren.cc/ if http://www.oakley-sunglasses.mex.com/ you http://www.hollister-clothingstore.com/ remember, http://spurs.nba-jersey.com/ the http://www.uggs-onsale.net/ only http://www.jordanrelease-dates.us.com/ way http://www.tommy-hilfiger.com.de/ to http://www.fendi-outlet.in.net/ learn http://www.pandoras-charms.org.uk/ distressed http://www.adidas-shoes.cc/ themselves http://www.ralph-laurenoutletonline.in.net/ in http://grizzlies.nba-jersey.com/ order http://www.vans-shoes.net/ to http://www.ray-banssunglasses.co.uk/ make http://www.uggsoutlet.net.co/ a http://jets.nfljersey.us.com/ good http://azcardinals.nfljersey.us.com/ wife, http://www.cheapjerseys.us.org/ good http://www.prada-handbags.net.co/ to http://www.long-champoutlet.com/ live.Eyes http://browns.nfljersey.us.com/ were http://eagles.nfljersey.us.com/ tears http://www.michael-kors.cc/ become http://www.coach-outletonline.ca/ increasingly http://www.newbalanceshoes.com.es/ clear, http://www.michaelkors.co.nl/ heart http://www.pradaoutlet.com.co/ pain http://www.tnf-jackets.us.com/ due http://www.designer-handbagsoutlet.us.com/ to http://www.omega-watches.com.co/ become http://www.puma-shoes.de/ even http://www.jimmy-chooshoes.com/ more http://www.nikefree-run.org.uk/ transparent, http://www.converse-shoes.net/ because http://www.nba-jersey.com/ life http://bengals.nfljersey.us.com/ experiences http://www.asicsoutlet.us.org/ become http://www.rayban.co.nl/ even http://giants.nfljersey.us.com/ more http://www.pandorajewellery.com.au/ abundance. http://www.truereligion-outlet.com.co/ Cried, http://www.christianlouboutinshoes.jp.net/ the http://www.longchamp-handbagsoutlet.us.com/ pain http://www.nhl-jerseys.us.com/ had http://www.beats-by-dre.com.co/ gone http://www.burberry-handbagsoutlet.net.co/ through, http://www.ralph-laurens.org.uk/ give http://thunder.nba-jersey.com/ yourself http://oakley.blackfriday.in.net/ a http://www.soccers-shoes.net/ sunny http://www.nikefreeshoes-inc.co.uk/ day, http://www.longchamp.com.co/ it http://www.chrome-hearts.in.net/ should http://www.designerhandbagsoutlet.net.co/ be http://www.barbour-factory.com/ understood, http://www.adidasshoes.org.es/ to http://lions.nfljersey.us.com/ a http://www.burberry-outletstore.net/

http://www.pandoracharms-canada.ca/ http://www.swarovski-crystals.us.com/ http://coach.blackfriday.in.net/ http://www.woolrich-clearance.com/ http://rams.nfljersey.us.com/ http://nets.nba-jersey.com/ http://www.ugg-bootscanada.ca/ http://www.michael-korsoutlet.cc/ http://www.ralphlaurenonlineshop.de/ http://www.michael-korsbags.org.uk/ http://www.ralphlaurens-outlet.co.uk/ http://www.swarovski-canada.ca/ http://www.ralph-laurenspolo.co.uk/ http://www.toms-shoe.us.com/ http://www.beatsbydrdrephone.com/ http://www.cheap-nike-shoes.net/ http://www.puma-shoesoutlet.com/ http://michaelkors.blackfriday.in.net/ http://www.michael-korsbags.co.uk/ http://celtics.nba-jersey.com/ http://www.ralphlaurencanada.ca/ http://www.truereligionjeans.net.co/ http://www.oakley-outlet.net.co/ http://www.omegawatches.in.net/ http://www.retro-jordans.com/ http://www.burberryonlineshop.de/ http://www.airyeezy.us.com/ http://www.thenorth-face.ca/ http://pistons.nba-jersey.com/ http://www.newbalance-outlet.org/ http://www.tommyhilfiger.net.co/ http://www.hermes-bags.net/ http://steelers.nfljersey.us.com/ http://www.swarovski-online-shop.de/ http://www.instylers.us.org/ http://www.polos-outletstore.net/ http://patriots.nfljersey.us.com/ http://www.hugo-bossoutlet.com/ http://www.oakley--sunglasses.com.au/ http://www.burberrybags-sale.net/ http://ravens.nfljersey.us.com/ http://www.cheapshoes.net.co/ http://www.abercrombie-andfitchs.com/ http://www.nike-huaraches.nl/ http://www.nfljersey.us.com/ http://www.toms-outlet.net/ http://www.ralphlauren-au.com/ http://www.mlb-jerseys.us.com/ http://www.nike-rosheruns.nl/ http://www.cheap-thomassabos.co.uk/ http://www.airmax-90.org/ http://www.rolex-watches.net.co/ http://www.the-northfacejackets.co.uk/ http://dolphins.nfljersey.us.com/ http://www.uggs-australia.com.co/

Post comment

Nick *
Your name or nickname
E-mail
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