Pelles C forum

C language => Windows questions => Topic started by: liut on July 29, 2013, 10:11:56 AM

Title: Console functions in windows
Post by: liut on July 29, 2013, 10:11:56 AM
Hello,

I recently used a set of console functions in my daemon program for some text output like this:

// Allocate a console
AllocConsole();

// Write some text
WriteConsole("something...");

// Close and free it when no use
FreeConsole();

Everything work fine. But if I click the "close" button or select "close" of sysmenu on the console window, the whole program exists immediately and unexpectedly. I found out a stupid way to disable the "close": GetSystemMenu() and DeleteMenu() on the "hwnd" of the console. It works, but not perfect:
1) if I right-click the console task on the task bar and select "close", I still can't prevent the program exiting;
2) actually I can't capture such kind message of program exiting yet, because the console has a different "hwnd" from the main program, and I don't know how to define another WndProc for this "hwnd"...

Please give any comments. Thanks!
Title: Re: Console functions in windows
Post by: liut on August 01, 2013, 04:54:38 AM
It looks no direct solution for it.

My current alternative: create an 'edit' sub-window, control an own buffer for output text and use setwindowtext() to update the content inside. Then I can capture the msg 'WM_CLOSE' to avoid the unexpected exitting.

I consider a solution for console functions too: create a sub-process for the console, send text info to this sub-process, control and monitor if it's living. It can avoid the issue too, but obviously make the thing complicated.
Title: Re: Console functions in windows
Post by: jj2007 on August 01, 2013, 10:26:57 PM
There is a long and frustrating thread by Raymond Chen (http://blogs.msdn.com/b/oldnewthing/archive/2007/12/31/6909007.aspx) about why CSRSS makes it impossible (or at least extremely difficult) to enter the console's window procedure.

The DeleteMenu() trick is not bad at all, at least it stops users from hastily closing the program. On the other hand, faking a console with an edit control is maybe the best solution, if you need the full control.