Click here to Skip to main content
13,247,891 members (46,356 online)
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
<!-- Download Links --> <!-- Article image -->

Sample Image - debugcon.gif

<!-- Add the rest of your HTML here -->


At first I want to apologize for my not very good English. Furthermore 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 how 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 ways how 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 terminating the whole application. You may disable the x-button at any time by calling DisableClose()
  • Return Value: true if successfull, 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 functions 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 consle 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 defaut:

	// 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 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

GeneralProblem with Show() Pin
moshezel23-Oct-07 9:42
membermoshezel23-Oct-07 9:42 
GeneralRe: Problem with Show() Pin
moshezel23-Oct-07 9:52
membermoshezel23-Oct-07 9:52 

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 | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 20 Aug 2002
Article Copyright 2002 by Greeeg
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid