Click here to Skip to main content
15,936,482 members
Articles / Programming Languages / C++

Console Output from a GUI program

Rate me:
Please Sign up or sign in to vote.
4.26/5 (16 votes)
8 Apr 2005CPOL4 min read 116.4K   1.5K   45   8
Describes how a Windows program can be output to a console window.

Sample Image - EditBin.gif


If you create a Windows program using the MFC Application Wizard, the program will not show text output which has been sent to the Standard Output or Standard Error handles.

Visibility of the output may be desirable at some point in time for diagnostic or functional purposes. Or you may wish to add command line operation of the program. Then to be consistent with command line operation, text should automatically be output to the command/dos window.

This article documents the use of EditBin utility to activate console mode operation of a Windows (or GUI ) program. (And for those who want some source code, the program contains an EditBin program relating to redirection of the cout stream object.)

Alternatives for obtaining text output

Run the Windows program from a command prompt and redirect the output using the redirection feature of the command window: ">out.txt" and "2>errors.txt"

Reassign the standard output and error output handles to be piped or sent to another file using ::SetStdHandle(). This process seems to be tricky to achieve.

You may write a console program that remaps the stdout, stdin, and stderr provided to a spawned program. The spawn program would be the one which has the output that you want to collect.

The Microsoft EditBin Utility

EditBin.exe is found in the VC98\bin subpath of Visual Studio 6.

This obscure tool offers the key for enabling text console output from Windows mode programs written in Visual C++ 6 (or Visual Basic 6). You can toggle the program executable between Windows mode and console mode.

  • EditBin MyProgram.exe /SUBSYSTEM:CONSOLE
  • EditBin MyProgram.exe /SUBSYSTEM:WINDOWS

Including the Editbin utility in the build process

The article image shows Visual Studio 6 Project Settings to show in situ how the Microsoft EditBin utility appears when added to become part of the build process.

The "Post-Build Step" tab of the Projects Settings dialog allows you to enter in a command on your final executable. The picture shows the example:


Side Effect for a Console version of the program

The console window will be appear along with the application window. Such may be the desired option if you want to view diagnostic information. If you start the program from a command prompt, the command window will show the Standard Output and Standard Error text.

You may have to create two copies of your application: a strictly GUI program, and a command line version. The programs will involve the same compiled code, but one will be modified using the EditBin tool.

Also a Microsoft help entry mentioned that the file handles 0,1 and 2 (that should be equivalent somehow to stdout and stderr) do not get reassigned since these values are assigned in the CRT startup of the Windows program. (Yet you may be able to modify the CRT using the files from the vc98\CRT\source directory.)

One last observation on the VS6 project settings...

You may notice "/subsystem:windows" as appears in an MFC Wizard generated application under the Project Settings Link tab. Yet console output can't be obtained by simply changing this entry to /subsystem:console.

Some code for redirection of cout

The source code program here, provides an example for redirecting the cout stream object. For some reason(s), the source code doesn't properly redirect the stdout file handle, though various websites describe that the stdout should be redirectable.

The application consists of a text editor window that is automatically filled with the time of day at ten seconds intervals. The program relies on piped output which is being read by a ReadFile call operating within a thread.

The heart of the redirection occurs within the initialization of the main application object. For all related code, search for the "April 2005" string which will lead you to all code modifications to the standard wizard generated code.

Initialization Steps

rval = CreatePipe(&hRead,&hWrite,0,0);
rval = SetStdHandle(STD_OUTPUT_HANDLE,hWrite);

//Now assign hWrite to stdout, cout
FILE *fp;
int hConHandle;
long lStdHandle;
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
// The stdout reassignment didn't seem to work
// *stdout = *fp;  // redefine the stdout to point to the pipe
setvbuf( fp, NULL, _IONBF, 0 );
 // Filebuf object attached to "test.dat"
filebuf *fb = new filebuf(_fileno(fp  ) );
cout = fb;

This program shows a little bit of how redirection can be done. Note that the buffering of text (for text being inserted into the RTF document) is not designed ruggedly. The code bypasses the need to use the Microsoft EditBin.exe tool in some but not all cases.

It would have been desirable to write the data to a memory file instead of sending data through a pipe. Then there would not have been a need to create a thread to watch the output of the pipe. Anyhow, the _open_osfhandle failed to convert a memory map handle to a typical file handle (in hConHandle).


The Windows GUI development doesn't conveniently support the access to cout, cerr, stdout, or stderr. It may be difficult to obtain these under direct control of the window program. At least there exist some tricks to obtain the information if really needed.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

Generalworked for VS2005 C# Pin
Alan P Brown25-Oct-11 0:00
Alan P Brown25-Oct-11 0:00 
Generalexcellent article - used this info in cppunit Pin
hushaar2-Oct-07 20:39
hushaar2-Oct-07 20:39 
Generalanother way Pin
codification7-May-06 16:43
codification7-May-06 16:43 
GeneralRe: another way Pin
Willem.D.6-Sep-06 8:11
Willem.D.6-Sep-06 8:11 
GeneralThanks. Pin
John M. Drescher29-Sep-05 10:44
John M. Drescher29-Sep-05 10:44 
GeneralSpelling correction & update Pin
Tom Brennan10-May-05 3:30
Tom Brennan10-May-05 3:30 
QuestionWhere is the project demo file? Pin
Abbas_Riazi10-Apr-05 7:29
professionalAbbas_Riazi10-Apr-05 7:29 
AnswerRe: Where is the project demo file? Pin
mikewhitney11-Apr-05 6:09
mikewhitney11-Apr-05 6:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.