Skip to main content
gamedev.net
🔒 Locked

Output console while using win32

Started by Metal Typhoon 7 replies 300+ views
Original Post
Metal Typhoon
Metal Typhoon
is there such way that u can open the console and output a variable while keep using your compiler for win32 ?
Metal Typhoon
dalleboy
dalleboy
Use AllocConsole to open a console, use WriteFile to print to the console.

    
AllocConsole();
const char* szMessage = "The message\nLalala\n";
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), szMessage, strlen(szMessage), NULL, NULL);


NOTE1: Only call AllocConsole once.
NOTE2: If you close the console, you close the other program also.
NOTE3: There is a way around NOTE2, using GetSystemMenu and EnableMenuItem.

EDIT1: Added notes.



Update GameDev.net system time campaign: 'date ddmmHHMMYYYY'

[edited by - dalleboy on January 3, 2003 2:17:07 PM]
Metal Typhoon
Metal Typhoon
can you give me a "why" that i have to close the app? please
Metal Typhoon
dalleboy
dalleboy
Opps, if you press the [X] in the upper right of the console, or if you press Alt-F4 on the console, the same thing will happen to the program that allocated the console. They are like siamese twins, if one die the other (most probably) also dies.

HTTP 500 strike 1
HTTP 500 strike 2



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
krez
krez
just out of curiosity, is there a way to uise this console with c++ iostream? i.e. "cout << blah;"? or must you use WriteFile()?
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
dalleboy
dalleboy
Hmm, I tried it just a minute ago, but it didn''t work. Perhaps a special iostream for that purpose? i.e. basic_icstream, basic_ocstream and basic_cstream for Console streams? I''ll have a look at it.



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
Oxyacetylene
Oxyacetylene
I found this code somewhere on the net (I forget where), it redirects printf, cout, cin, etc to a console created with AllocConsole


  
using namespace std;

AllocConsole();

int hConHandle;
long lStdHandle;
FILE* fp;

// redirect unbuffered STDOUT to the console


lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );

// redirect unbuffered STDIN to the console

lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );

// redirect unbuffered STDERR to the console

lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );


// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog

// point to console as well

ios::sync_with_stdio();



Then when you're done


  
FreeConsole()


[edited by - Oxyacetylene on January 3, 2003 3:17:26 PM]
krez
krez
Oxyacetylene: i get an error on the line "stdout = *fp;". it says "lvalue required on left side"...
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
risingdragon3
risingdragon3
if you want a ostream iostream not using Oxyacetylene's code:
Writing a simple stream_buf to allow iostreams with a console window is not in and of itself terribly difficult.
[begin plug]
For example, libGDN has an stream_buf that outputs to any FILE*, and also contains a stream_buf adaptor that makes writing stream_buf's very easy: just implement Write() and your done. Have a look at the CVS tree and the examples, io*.
Now, libGDN doesn't currently have a console stream, but I've been working on that and format manipulators - color, fore and back, underline, etc. and possibly positional - that the win32 API allows you to mess with. It shoudl be done once I get this damn windowing system to work correctly...
[end plug]

Now, if you dont' wanna use the libGDN adaptor, have a look at stream_buf - you'll need to implement overflow() I think to call Write() on the console. Then subclass basic_ostream and in the constructor, call basic_ostream(&console_buffer); where consolebuffer is an instance of your class.
There's tons of info on the net; try a google.

Ooops: libgdn: here
[edited by - risingdragon3 on January 3, 2003 8:03:33 PM]

[edited by - risingdragon3 on January 3, 2003 8:04:10 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.