🔒 Locked
Output console while using win32
Advertisement
Original Post
is there such way that u can open the console and output a variable while keep using your compiler for win32 ?
Use
NOTE1: Only call
NOTE2: If you close the console, you close the other program also.
NOTE3: There is a way around NOTE2, using
EDIT1: Added notes.
Update GameDev.net system time campaign:
[edited by - dalleboy on January 3, 2003 2:17:07 PM]
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]
can you give me a "why" that i have to close the app?
please
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:
HTTP 500 strike 1
HTTP 500 strike 2
Update GameDev.net system time campaign:
''date ddmmHHMMYYYY''
I found this code somewhere on the net (I forget where), it redirects printf, cout, cin, etc to a console created with AllocConsole
Then when you're done
[edited by - Oxyacetylene on January 3, 2003 3:17:26 PM]
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]
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]
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.
Advertisement