www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How can a console app detect when it is being closed?

reply Don Clugston <dac nospam.com.au> writes:
Suppose I have the trivial console app:

import std.stdio;

static ~this()
{
     writefln("Exiting program");
}

void main()
{
     scope (exit) writefln("Exiting scope");
   for (;;) {
     writefln("xxx");
  }
}

When you press ctrl-C, neither of the exit messages are printed.
What I'd like to do is pop up an "Are you sure?" message box on any 
attempt to close the program. I could create an invisible window from 
the console app, but I'm hoping for something less ugly.  I just don't 
know how to intercept WM_QUIT messages from a console app -- any ideas?
Aug 01 2006
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Don Clugston wrote:
 
 Suppose I have the trivial console app:
 
 import std.stdio;
 
 static ~this()
 {
     writefln("Exiting program");
 }
 
 void main()
 {
     scope (exit) writefln("Exiting scope");
   for (;;) {
     writefln("xxx");
  }
 }
 
 When you press ctrl-C, neither of the exit messages are printed.
 What I'd like to do is pop up an "Are you sure?" message box on any 
 attempt to close the program. I could create an invisible window from 
 the console app, but I'm hoping for something less ugly.  I just don't 
 know how to intercept WM_QUIT messages from a console app -- any ideas?
Here's a way to do it with C++: http://www.codeproject.com/win32/console_event_handling.asp?print=true (I just googled for console events ..)
Aug 01 2006
parent Don Clugston <dac nospam.com.au> writes:
Hasan Aljudy wrote:
 
 
 Don Clugston wrote:
 Suppose I have the trivial console app:

 import std.stdio;

 static ~this()
 {
     writefln("Exiting program");
 }

 void main()
 {
     scope (exit) writefln("Exiting scope");
   for (;;) {
     writefln("xxx");
  }
 }

 When you press ctrl-C, neither of the exit messages are printed.
 What I'd like to do is pop up an "Are you sure?" message box on any 
 attempt to close the program. I could create an invisible window from 
 the console app, but I'm hoping for something less ugly.  I just don't 
 know how to intercept WM_QUIT messages from a console app -- any ideas?
Here's a way to do it with C++: http://www.codeproject.com/win32/console_event_handling.asp?print=true (I just googled for console events ..)
Thanks! That will do quite nicely.
Aug 01 2006