This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  C/C++ Console
  Submitted by



During my programming machinations, I often find that I want to do things that the debugger can't do for me, such as keep a long "printout" of numbers to watch how a variable changes, or to figure out why my program is crashing in release mode, but not in debug mode. Accordingly, I wrote a small program that sits in the system tray. This program, called CONSOLE.EXE simply sits around in the tray and waits for an application to send messages to it, which it then displays. Since it's in the tray, you can program away, and only pop it open when you want to check something out. To go with CONSOLE.EXE, I offer CONSOLE.CPP and CONSOLE.H. To use them in your program, just add the cpp and h file to your project, and declare a console wherever you want, such as: Console gConsole; (I recommend making it global and including the headers in your 'all included' header file) Then, if you want to print something out during development, you just say:
gConsole.Out("Hey, it works!  That John Raptis guy is pretty amazing.");
gConsole.Out("That %s is so amazing, I want to give him $%.2f dollars","John Raptis",100.00f); 

or, my favorite...
gConsole.Out("The program made it this far...");
.... [code] ....
gConsole.Out("The program made it this far too...");
.... [code] ....
gConsole.Out("The program still hasn't crashed...");
.... [code] ...
gConsole.Out("This module is okay!"); 

It accepts all the sprintf formatting, etc. It doesn't handle std::string right now, but you can always send in a std::string.c_str() to it. You can also have it dump to a file instead of to the console in the tray by uncommenting the #define FILE in the .h file. Note that if you don't have CONSOLE.EXE running, it'll give you a message about how the console output window wasn't found, and no messages will be recorded. I primarily put that in because of my own absentmindedness, feel free to trim that out. This code is free to anyone who wants to use it, and I hope it helps track down things that might have otherwise wrangled your brain.

Download: cpp_console.zip John Raptis
Raptisoft

Currently browsing [cpp_console.zip] (8,594 bytes) - [Console/Console.cpp] - (2,101 bytes)

// Console.cpp: implementation of the Console class.
//
//////////////////////////////////////////////////////////////////////


#include <stdarg.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string> #include <memory.h> #include <dos.h> #include <math.h> #include <fcntl.h> #include <io.h> #include <sys\types.h> #include <sys\stat.h>

#include <malloc.h> #include <float.h> #include <windows.h> #include <mmsystem.h> #include <process.h> #include <assert.h> #include <commctrl.h> #include <commdlg.h> #include <objbase.h>

#include "Console.h"

////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// Console::Console() { #ifndef FILE

mHwnd=NULL;

mHwnd=(long)FindWindow(NULL,"C++ Console"); if (mHwnd==NULL) MessageBox(0,"The C++ Console was not found. Console reporting will be lost.","No C++ Console!",MB_OK); else { COPYDATASTRUCT copy; copy.cbData=0; copy.lpData=NULL; SendMessage((HWND)mHwnd,WM_COPYDATA,NULL,(long)©);

Out("*** BEGIN SESSION ***"); }

#else mHwnd=creat("DEBUG.TXT",S_IREAD|S_IWRITE);close(mHwnd); Out("*** BEGIN SESSION ***"); #endif }

Console::~Console() { Out("*** END SESSION NORMALLY ***"); }

void Console::Out(char *format, ...) { #ifndef FILE if (mHwnd==NULL) return;

char *xstring=(char*)GlobalAlloc(GMEM_FIXED,2048);

va_list argp; va_start(argp, format); vsprintf(xstring,format, argp); va_end(argp);

COPYDATASTRUCT copy; copy.cbData=strlen(xstring)+1; copy.lpData=xstring;

SendMessage((HWND)mHwnd,WM_COPYDATA,NULL,(long)©);

GlobalFree(xstring); #else mHwnd=open("DEBUG.TXT",O_BINARY|O_RDWR); lseek(mHwnd,0,SEEK_END);

char *xstring=(char*)GlobalAlloc(GMEM_FIXED,2048);

va_list argp; va_start(argp, format); vsprintf(xstring,format, argp); va_end(argp);

char crlf[2]={13,10};

write(mHwnd,xstring,strlen(xstring)); write(mHwnd,crlf,2); close(mHwnd); #endif }


Currently browsing [cpp_console.zip] (8,594 bytes) - [Console/Console.h] - (587 bytes)

// Console.h: interface for the Console class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_CONSOLE_H__08398910_4C1F_4791_9F37_76F6D8301CFB__INCLUDED_)
#define AFX_CONSOLE_H__08398910_4C1F_4791_9F37_76F6D8301CFB__INCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 //#define FILE class Console { public: Console(); virtual ~Console();

void Out(char *format, ...);

private: long mHwnd; };



#endif // !defined(AFX_CONSOLE_H__08398910_4C1F_4791_9F37_76F6D8301CFB__INCLUDED_)

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.