NO

Author Topic: Console functions in windows  (Read 3460 times)

liut

  • Guest
Console functions in windows
« 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!

liut

  • Guest
Re: Console functions in windows
« Reply #1 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.

Offline jj2007

  • Member
  • *
  • Posts: 536
Re: Console functions in windows
« Reply #2 on: August 01, 2013, 10:26:57 PM »
There is a long and frustrating thread by Raymond Chen 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.