Solved

windows.h console functions, smitty1276

Posted on 2001-06-05
13
1,114 Views
Last Modified: 2013-12-14
please take a look at the class below and suggest how i can display variables (i.e. integer vars, string vars, etc.) can the print function be overloaded to handle other data types?

// console.h
#ifndef CONSOLE_H
#define CONSOLE_H

#include <windows.h>

class console
{
public:
     console();
     void conInit   ();
     void cls       (void);
     void print     (char *text);
     void gotoxy    (short x, short y);
     void fontColor (WORD attr);
private:
     HANDLE scr;
};

#endif

// console.cpp
#include "console.h"

console::console()
{
     conInit();
}

void console::conInit(void)
{
    scr = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CONSOLE_TEXTMODE_BUFFER, 0 );
    SetConsoleActiveScreenBuffer( scr );
}

void console::cls(void)
{
     CloseHandle(scr);
     scr = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CONSOLE_TEXTMODE_BUFFER, 0 );
     SetConsoleActiveScreenBuffer( scr );
}

void console::print(char *text)
{
     char buf[1024];
     strcpy(buf, text);
     DWORD num;
     WriteConsole( scr, buf, strlen(text), &num, 0);
}

void console::gotoxy(short x, short y)
{
     COORD pos = { y, x };
     SetConsoleCursorPosition(scr, pos);
}

void console::fontColor(WORD attr)
{
     SetConsoleTextAttribute(scr, attr);
}

// consoleMain.cpp
#include "console.h"

#include <string>
using std::string;

void main()
{
     int num = 5;
     sting name;
     name = "kiphughes";

     console A;
     A.cls();
     A.conInit();
     A.gotoxy(5,5);
     A.fontColor(FOREGROUND_RED);
     A.print("This is a test");
     A.print(name);                    // won't print string
     A.print(num);                    // won't print int
     A.print(num + num);               // won't print calculations
     A.cls();
}
0
Comment
Question by:kiphughes
13 Comments
 
LVL 10

Expert Comment

by:makerp
Comment Utility

    void print     (int num);

    void print     (char *text);

    void print     (float number);

then just implement them, c++ supports method overloading, the parameters must be different
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Some suggestions...

Your must remember to call close. If you failt to call close you will cause a resource leak.  Dangerious.  Instead give the class a destructor and have it call close..  Pluse the close function "reopens"   I assume that is a mistake.  if not that is another memory leakl.  A good safe  system woudl be

console::console() :
   scr(INVALID_HANDLE_VALUE) // Indicate class is colose
{
   conInit();
}

console::~console()
{
   cls();// Make sure class is closed.
}


void console::conInit(void)
{
   if (!Isclosed())  // If called when open
      cls();             // be sure to close first.
   scr = CreateConsoleScreenBuffer( GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
    0, CONSOLE_TEXTMODE_BUFFER, 0 );
    SetConsoleActiveScreenBuffer( scr );
}

void console::cls(void)
{
   CloseHandle(scr);
   scr = INVALID_HANDLE_VALUE;
}

bool console::IsClosed() const
{
    return scr == INVALID_HANDLE_VALUE;
}

Now it is still "unsafe" to use this class for output when it is closed.  It will simply ignore the requested operation in this case, it will not act unpredictably, so its an improvement over yoru version.


Your print function will fail to print anything longer than 1k.  sometimes that sort of limit is needed, but in this case it was totally arbitrary because you added an unnecessary copying of the data.  Try.

oid console::print(const char *text)
{
   DWORD num;
   WriteConsole( scr, text, strlen(text), &num, 0);
}l

Now you can overload the output functiosn as markerp suggested, but I would recommend using operator <<

0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
for example,

console &operator << (console &Con,const char *Dat)
{
   con.print(Dat);
   return Con;
};

console &operator << (console &Con,int Dat)
{
   char Str[32];

   sprintf(Str,"%i",Dat);
   con.print(Str);
   return Con;
};

console &operator << (console &Con,double Dat)
{
   char Str[32];

   sprintf(Str,"%e",Dat);
   con.print(Str);
   return Con;
};

console &operator << (console &Con,char Dat)
{
   char Str[2] = "?";

   Str[0] = Dat;;
   con.print(Str);
   return Con;
};


Now you can do things like

console C;

C << "Label:" << ' ' << 123 << ' ' << 123.456;

0
 
LVL 2

Expert Comment

by:smitty1276
Comment Utility
Actually, that wasn't intended to be a close function.  It was supposed to be a clear screen function (I just stole the old BASIC function name).  

Like I said before... I haven't used ConsoleScreenBuffer objects all that much myself.  When I posted those functions I specified that there was probably a better way to clear the screen.  I just found that closing the handle and reopening it was the quickest to code, and it seemed to do the job.

You bring up a good point though. There needs to be a method that will close the buffer and deallocate the memory.
0
 
LVL 1

Author Comment

by:kiphughes
Comment Utility
how does this display a string?

example:

console C;
string str = "kiphughes";

C << str;
0
 
LVL 1

Author Comment

by:kiphughes
Comment Utility
nietod,

also, where am i suppose to put the code u posted? do i put it in the console class? or do i put that in a different class that will use the overloaded operators? either way, what must i put in the .h file? i think i'd rather have this in my console class.

btw, what's the difference here?
console &operator << (console &Con,char Dat)
console &operator << (console &Con,const char *Dat)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> lso, where am i suppose to put the code u posted?
the operator << functions are free functions (not members of the console class.  that is why tha don't have "console::" in there names.  The rest is in the console class, that is why they have "Console::" in their names.

>> what must i put in the .h file?
The changes to the console class and the operator << functions, but those go outside of the class.

>> console &operator << (console &Con,char Dat)
>> console &operator << (console &Con,const char *Dat)
the first outputs a character, the 2nd poiputs a NUL termianted strign of charcters, like

SomeConsole << 'A';  // Single charcter.
SomeConsole << "A"; // String of 1 character.

For those two cases the differences doesn't seem important, but there will probably be times when you have only a single character as a varaible and need to ouput it and you don't want to have to form a NUL termianted string from it.
0
 
LVL 1

Author Comment

by:kiphughes
Comment Utility
what about displaying a string?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
That is what the 2nd one does.   I could be a string of 1 character or a string of 1000.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Oh you mean a STL string object.

console &operator << (console &Con,const string &Str)
{
   con.print(Str.c_str());
   return Con;
};

i.e. to print anyting, pass it to operator <<.  then convert that thing to a string and pass the converted string to prnt().
0
 
LVL 1

Author Comment

by:kiphughes
Comment Utility
it won't work for some reason, i get this message:

error C2664: 'print' : cannot convert parameter 1 from 'const char *' to 'char *'
        Conversion loses qualifiers
Error executing cl.exe.

this is how the string is declared:

#include <string>
using std::string;

string str;
str = "kiphughes";
0
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
             error C2664: 'print' : cannot convert parameter 1 from 'const char *' to 'char *'
the problem is in print().  It should be declared to take a parameter that is a constant character pointer.  I did that above.  print doesn't change the string so there is no loss n functionallity by making this explicit, by adding the const.  But when you make it constant you increase the usability of the function.  it can be used with non-constant string AND with constant strings.  (your version could only be used with non-constant strings).   This more than doubles the usability of the function since most strings actually will be constant.
0
 
LVL 1

Author Comment

by:kiphughes
Comment Utility
i got it working, thanx!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now