Archived

This topic is now archived and is closed to further replies.

How do I get a console window with a Win32 app?

This topic is 5503 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

I have a simple Win32 application that does nothing more than display a small window. Can I also get the program to open a console window (dos box), which I can print debug output to? Is it fairly simple, or very involved? Thanks http://www.stodge.net - the powerhouse in personal commentary

Share this post


Link to post
Share on other sites

    
int con = 0;

// Create the console...

if (con = AllocConsole ()) {
freopen ("CONOUT$", "w", stdout);
freopen ("CONIN$", "r", stdin);
}

// Print stuff...

printf ("Hello world!\n");

// Later, when you're done with it...

if (con)
FreeConsole ();


[edited by - bishop_pass on November 19, 2002 2:34:25 AM]

Share this post


Link to post
Share on other sites
The big problem with AllocConsole is that little close button on the console window. Click that and your app terminates and there isn''t a damn thing you can do about it. It would be a very simple and elegant tool if you could just trap that button and make it close the window instead of killing the app.

Share this post


Link to post
Share on other sites
I haven''t tried it, but you might be able to set up a handler routine (using SetConsoleCtrlHandler()) to trap CTRL_CLOSE_EVENT events and prevent the console from being closed.

Share this post


Link to post
Share on other sites
It doesn''t look like it''s possible. Windows interprets clicking the X button as the user''s intention to terminate the process.

No way around it using AllocConsole and FreeConsole()

Share this post


Link to post
Share on other sites