Click here to Skip to main content
13,592,615 members
Click here to Skip to main content
Add your own
alternative version


75 bookmarked
Posted 19 Aug 2002

CConsole - A Simple Console for Debug Output

, 19 Aug 2002
Rate this:
Please Sign up or sign in to vote.
Show debug information in an easy to handle console

Sample Image - debugcon.gif


This is my first article on CodeProject. Probably the next time, I will know what to do better if this article is poor ;-) It's not an advanced article, but I hope some people will find it useful anyway. If you find any mistakes or know ways to improve my coding style, then let me know! There's still a lot I need to learn, and the best way is to do it on


A few days ago, I was searching for a way to provide simple debug information in my Release builds, so that people who download my program have the ability to watch the debug output as well and send me the logs if something's going wrong in my program. There are quite a lot of ways to achieve this. One would be to create a nonmodal window with a listview control placed on it to write the debug information to. But I found some very interesting API calls which deal with consoles. That's why I wrote my own simple wrapper class for some console functions like AllocConsole, WriteConsole and so on. My main goal was to keep the wrapper class as simple as possible, so that everybody could easily use this class in his/her applications.

The CConsole Class

The CConsole wrapper class currently consists of 11 functions:

bool   Create(const char* szTitle, bool bNoClose = false);
void   Color(WORD wColor = NULL);
void   Output(const char* szOutput = NULL, ...);
void   SetTitle(const char* szTitle);
char*  GetTitle();
HANDLE GetHandle();
void   ShowConsole(bool bShow = true);
void   DisableClose();
void   Clear();
void   Close();

I'm going to explain them in detail now:

bool   Create(const char* szTitle = NULL, bool bNoClose = false);

This function will create the console and display it on the screen. Parameters:

  • const char* szTitle = NULL - The title for the console window, can be set to NULL
  • bool bNoClose = false - If set to true, the [x] button of the console window will be disabled so that it is no longer possible to close the console and thus terminate the whole application. You may disable the x-button at any time by calling DisableClose()
  • Return Value: true if successful, false otherwise
void   Color(WORD wColor = NULL);

This function changes the color of the output text and console background. Parameters:

  • WORD wColor = NULL - Allows any combination of FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_BLUE, FOREGROUND_INTENSITY and BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE. For more information, see SetConsoleTextAttributes() in the MSDN library.
    If left blank, the colors will be reset to the defaults (white text on black background).
void   Output(const char* szOutput = NULL, ...);

This function writes output to the console. You may enter a simple string or even a formatted string as known from printf(). (e.g. con.Output("This is some %s with number %i", string, 14) will result in This is some string with number 14. NOTE that you must put a newline \n character at the end of the string if you want to proceed with a new line. Alternatively, you can call Output() with the parameter left blank. This will write a new line as well.

void   SetTitle(const char* szTitle);

You may set the title of the console at any time like that: SetTitle("New Title");

char*  GetTitle();
HANDLE GetHandle();

These functions allow you retrieve some properties of the console like its title, window handle or the console handle.

void   Show(bool bShow = true);

Call this function if you want to show/hide the console. Set bShow to true to show, false to hide the console.
NOTE: This does NOT delete the console. If you want to delete the console, call Close().

void   DisableClose();

This will disable the [x] button of the console, so that you can't close it (and thus closing the whole application).

void   Clear();

Use this function if you want to clear the debug output. Note that there is no chance to bring back the previous output if you call this function!

void   Close();

This function will destroy the console. All output will be gone and calling any other functions from CConsole will not be possible unless you set up a new console with CConsole::Create().

How to Use It

Add a member variable CConsole m_Console or something like that to your main application.
Next, Create() the console. For example, this can be done in the OnInitDialog() handler in MFC apps or any other initialization code:

BOOL CConsoleDlg::OnInitDialog()


	// Create a console with title "Debug output window" and a disabled close button
	if(!m_Console.Create("Debug output window", true))
	    // insert code for the case the console could not be created

	return TRUE;

Once the console is set up, you may call the other functions:

// some button is pressed
void CConsoleDlg::OnButtonTest()
	// set the color of the output to an intense red font and blue background
	// this is really not advisable ;-)

	// write some output to the console
	// without '\n' at the end of the string this will NOT write a new line
	m_Console.Output("Test string %s number %i",szSomeString, count");

	// insert new line

	// set color to default:

	// write output WITH new line

	// set title of console
	m_Console.SetTitle("New Title");

	// hide the console

	// and so on...

Don't forget to close your console with m_Console.Close() when your app is about to terminate.

About the Code

This class is completely written by myself, EXCEPT the code for clearing the console. This was taken from the Microsoft Knowledge base. You can find the article here. The class should work both in MFC and non-MFC applications. Feel free to do what you want with this code, modify it, extend it to your needs. But do not remove my name from the top of the source files.


This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


About the Author

Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralDebug Console Use Pin
Samuel Batista9-Oct-08 12:57
memberSamuel Batista9-Oct-08 12:57 
GeneralClosing Console Pin
berlimbo24-Jan-08 20:04
memberberlimbo24-Jan-08 20:04 
GeneralProblem with Show() Pin
moshezel23-Oct-07 8:42
membermoshezel23-Oct-07 8:42 
GeneralRe: Problem with Show() Pin
moshezel23-Oct-07 8:52
membermoshezel23-Oct-07 8:52 
Generalthere is a big bug: Pin
koda_xii25-May-07 0:29
memberkoda_xii25-May-07 0:29 
GeneralConsole input Pin
drutherf17-May-06 6:07
memberdrutherf17-May-06 6:07 
Generalfunny bug Pin
curious_ambitious16-Dec-05 1:09
membercurious_ambitious16-Dec-05 1:09 
GeneralRe: funny bug Pin
Greeeg19-Dec-05 5:27
memberGreeeg19-Dec-05 5:27 
GeneralNice... but how to save the debug message Pin
xnetec15-Dec-05 3:32
memberxnetec15-Dec-05 3:32 
GeneralRe: Nice... but how to save the debug message Pin
Greeeg19-Dec-05 5:38
memberGreeeg19-Dec-05 5:38 
GeneralNice.. Pin
MoFo2-Mar-04 3:40
memberMoFo2-Mar-04 3:40 
GeneralNice Pin
Mike Klimentiev27-Aug-02 9:46
memberMike Klimentiev27-Aug-02 9:46 
GeneralRe: Nice Pin
Gregor S.29-Aug-02 7:18
memberGregor S.29-Aug-02 7:18 
GeneralRe: Nice Pin
Anonymous8-Nov-02 0:09
sussAnonymous8-Nov-02 0:09 
Generalconsole window & stdio Pin
Paolo Messina22-Aug-02 0:47
memberPaolo Messina22-Aug-02 0:47 
GeneralRe: console window & stdio Pin
Gregor S.22-Aug-02 6:53
memberGregor S.22-Aug-02 6:53 
GeneralRe: console window & stdio Pin
Forogar31-Jan-03 6:38
memberForogar31-Jan-03 6:38 
GeneralRe: console window & stdio Pin
moshezel23-Oct-07 8:33
membermoshezel23-Oct-07 8:33 
GeneralSuggestion Pin
Jon Taylor20-Aug-02 23:28
memberJon Taylor20-Aug-02 23:28 
GeneralRe: Suggestion Pin
Gregor S.21-Aug-02 2:16
memberGregor S.21-Aug-02 2:16 
GeneralRe: Suggestion Pin
Jon Taylor21-Aug-02 21:41
memberJon Taylor21-Aug-02 21:41 
GeneralRe: Suggestion Pin
Gregor S.22-Aug-02 6:59
memberGregor S.22-Aug-02 6:59 
GeneralRe: Suggestion Pin
Jon Taylor29-Aug-02 3:51
memberJon Taylor29-Aug-02 3:51 
GeneralRe: Suggestion Pin
Gregor S.29-Aug-02 7:14
memberGregor S.29-Aug-02 7:14 
GeneralRe: Suggestion Pin
Jaime Olivares18-May-05 5:09
memberJaime Olivares18-May-05 5: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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.180618.1 | Last Updated 20 Aug 2002
Article Copyright 2002 by Greeeg
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid