Killing a console window created using AllocConsole() in an MFC GUI app

Killing a console window created using AllocConsole() in an MFC GUI app

Post by Shehrz » Wed, 25 Sep 2002 08:07:53



Hello all,

There have been numerous postings about how to use printf() and cout
in the context of a GUI application.  I have done such a thing in my
application, but I seem to have run into a stumbling block.  I have a
menu item that hides or shows my "log" window, which is nothing more
than a console created via AllocConsole().  Now what happens is if the
user closes the console window by clicking on the "x marks the spot
close area" up there in the upper right-hand corner my entire
application exits!  I would prefer to prevent this from occurring,
either one of two ways:

(a) Is there some Windows configuration that prevents the
minimize/maximize/close title-bar areas from appearing in console
windows?  Barring that, is there something in AllocConsole() or the
related console API that will prevent these from appearing?  Is there
a windows hook method I could use?

(b) Is there a way to catch the close event occurring, and then
perhaps re-AllocConsole() a new console?

To provide some more context, my MFC app spins off a little thread
that calls AllocConsole.  In this thread I use the FindWindow() Win32
function to grab and store an HWND, which I subsequently use to
hide/show the window depending on GUI menu callbacks.  My thread proc
is quite simple :

while (m_bRunning)

  WriteFile(hConOut, ...);

FreeConsole();

Of course, I've tried setting a breakpoint in a destructor, which sets
m_bRunning to FALSE, and then closed my console window.  The entire
application still terminates and the de* never hits the
breakpoint.

Any help/advice would be most appreciated.  I really don't want to
slap together a GUI entity that shuttles STDOUT and STDERR onto a rich
text-edit control.

-Shehrzad

 
 
 

Killing a console window created using AllocConsole() in an MFC GUI app

Post by Tim Robinso » Wed, 25 Sep 2002 08:14:58


| (a) Is there some Windows configuration that prevents the
| minimize/maximize/close title-bar areas from appearing in console
| windows?  Barring that, is there something in AllocConsole() or the
| related console API that will prevent these from appearing?  Is there
| a windows hook method I could use?

Does installing a routine via SetConsoleCtrlHandler have any effect?

--
Tim Robinson
http://www.themobius.co.uk/

 
 
 

Killing a console window created using AllocConsole() in an MFC GUI app

Post by Jerry Coffi » Wed, 25 Sep 2002 13:00:33




Quote:> Hello all,

> There have been numerous postings about how to use printf() and cout
> in the context of a GUI application.  I have done such a thing in my
> application, but I seem to have run into a stumbling block.  I have a
> menu item that hides or shows my "log" window, which is nothing more
> than a console created via AllocConsole().  Now what happens is if the
> user closes the console window by clicking on the "x marks the spot
> close area" up there in the upper right-hand corner my entire
> application exits!  I would prefer to prevent this from occurring,

My advice would be to simply not use a real console window in the
first place.  A list box often works well as a log window, and it's
trivial to put a list box in a window without a system menu, which
handily eliminates the "close window" button as well.

--
    Later,
    Jerry.

The universe is a figment of its own imagination.

 
 
 

Killing a console window created using AllocConsole() in an MFC GUI app

Post by Shehrz » Thu, 26 Sep 2002 03:08:43




> | (a) Is there some Windows configuration that prevents the
> | minimize/maximize/close title-bar areas from appearing in console
> | windows?  Barring that, is there something in AllocConsole() or the
> | related console API that will prevent these from appearing?  Is there
> | a windows hook method I could use?

> Does installing a routine via SetConsoleCtrlHandler have any effect?

Thanks for the suggestion Tim!  In my console handler function I
simply print out a log message and return TRUE.  Now if the user tries
to kill the console window, Windows pops up a dialog saying it cannot
perform the requested action.  *Exactly* what I needed.

-SQ