Output log into a console window...
Hi,
Has always, until now, been generating logs into a logfile on disc.
Recently me and a friend started a discussion if it was possible to somehow, from a win32 app, open a Console window and ouput log strings directly at runtime into it, via "cout<<" or "fprintf"(Instead of just open a new window and using TextOut...).
Anyone here has a clue about this?
Thanks in advance!
Regards
/Tooon - OTB
November 07, 2000 02:14 PM
You can do it but it''s a pain. Fire up MSDN and look up "Console Functions". The basic procedure is to use AllocConsole to create the console window, GetStdHandle to get a Win32 handle to stdout, and finally get printf, cout, and friends to use that handle (I''m kinda fuzzy on that step).
The biggest problem IMHO is that it''s really hard not to click the close button on the console window. But if you do that then your whole app closes, not just the console window, and last time I tried there wasn''t any way to stop it. There''s also no easy way to get rid of the button.
Good luck.
-Mike
The biggest problem IMHO is that it''s really hard not to click the close button on the console window. But if you do that then your whole app closes, not just the console window, and last time I tried there wasn''t any way to stop it. There''s also no easy way to get rid of the button.
Good luck.
-Mike
November 07, 2000 02:54 PM
After you have called GetStdHandle to retrieve the stdout/stdin handles of the newly created console window I think you have to use the functions WriteConsole() and ReadConsole() to write/read. You cold of course create wrapperfunctions for printf etc...
like:
This was written from the top of my head, so it probably contains a few mistakes, but you should get the general idea...
Does anyone know of a way to get a "low-level handle" (not sure what they''re called) from a HANDLE (one that can be used in dup() for example.
like:
HANDLE stdout_handle; // global handle, open this elsewhere// conprintf returns the number of chars writtenint conprintf(char * fmt, ...){ va_list vp; char buf[1024]; int chars_written = 0; va_start(fmt, vp); _vsnprintf(buf, 1024, fmt, vp); WriteConsole(stdout_handle, buf, 1024, &chars_written, NULL); va_end(vp); return chars_written;};
This was written from the top of my head, so it probably contains a few mistakes, but you should get the general idea...
Does anyone know of a way to get a "low-level handle" (not sure what they''re called) from a HANDLE (one that can be used in dup() for example.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement