Skip to main content
gamedev.net
🔒 Locked

Console window for debugging with a window app?

Started by warp X Mar 16, 2005 at 10:38 AM 7 replies 4.9k views
Original Post
warp X
warp X
Is there any way to bring up the same console window, like with plain console applications, to screen when you are doing a windowed app with C++ (VS 2003)? It might help with debugging.
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
Drew_Benton
Drew_Benton
Yes, this is possible. ALl you need to do is change the 'subsystem' of your project. This can be done in project settings or you can use this:
#pragma comment(linker, "/subsystem:\"console\" /entry:\"WinMainCRTStartup\"")


Here's an image as proof [grin]

- Drew
warp X
warp X
Thx u2! Both slns work.
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
warp X
warp X
U know, this kind of forum is sometimes SO much better than google etc...
"I got stuck between levels 4 and 5 and got into this strange place: warp X..."
mfawcett
mfawcett
I usually like using the subsystem method, but if you would also like all the STD streams routed to that console, here is some code to do that for you.

This will bind cin, cout, cerr, printf, etc to the newly allocated console.

void CreateConsoleLog(const char *winTitle){	AllocConsole();	SetConsoleTitle(winTitle);	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 );}
--Michael Fawcett
Drew_Benton
Drew_Benton
Quote:
Original post by mfawcett
I usually like using the subsystem method, but if you would also like all the STD streams routed to that console, here is some code to do that for you.

This will bind cin, cout, cerr, printf, etc to the newly allocated console.

*** Source Snippet Removed ***


What is the difference between that method and using the subsystem? Everything that is being routed to the allocated console *seems* to already be routed with the subsystem method. Here's another pic. Or, is it just the 'proper' way to do it? Just curious [smile]
Evil Steve
Evil Steve
Quote:
Original post by Drew_Benton
What is the difference between that method and using the subsystem? Everything that is being routed to the allocated console *seems* to already be routed with the subsystem method. Here's another pic. Or, is it just the 'proper' way to do it? Just curious [smile]
The subsystem method forces you to have a log window when your application starts. Using AllocConsole() you can create the console at any time you want - for instance after you get into the actual game and not at the menu, or if you want it in debug mode only (although the subsystem method would be better suited in that case)
Drew_Benton
Drew_Benton
Quote:
Original post by Evil Steve
The subsystem method forces you to have a log window when your application starts. Using AllocConsole() you can create the console at any time you want - for instance after you get into the actual game and not at the menu, or if you want it in debug mode only (although the subsystem method would be better suited in that case)


Ohhh ok, thanks! Yes, I see now, more flexibility [smile].

Topic Locked

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

Sign in to reply to this topic.