Jump to content
  • Advertisement
Sign in to follow this  
warp X

Console window for debugging with a window app?

This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
AllocConsole

Share this post


Link to post
Share on other sites
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 );
}


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!