how to catch close signal ?

I know that the signal() function is used for catching a signal such as SIGTERM, SIGINT and so on, but I don't know how to catch the signal when user press 'x' (close the window) at windows system menu. any idea ?
[228 byte] By [Butterfly] at [2007-11-20 1:14:43]
# 1 Re: how to catch close signal ?
I doubt it is a signal but instead it is a system message. How to do that depends on what gui library was used to create the program, for example is it QT, X11, or something else?
stober at 2007-11-9 1:07:28 >
# 2 Re: how to catch close signal ?
I've tried to catch 'WM_QUIT' and 'WM_CLOSE', but it doesn't work. I don't know which signal is sent when we close the console application by pressing 'x' at windows system menu. I think this is abnormal terminate program, but how to deal with it ?
Butterfly at 2007-11-9 1:08:28 >
# 3 Re: how to catch close signal ?
As stober pointed out, no signal is sent, the window management is done via events or messages, handling of which is specific for the graphics toolkit you are using. We need more information - what system is this on, linux, bsd, mac os X, or other ? What graphics toolkit are you using for creating the window - pure X11, Qt, gtk, sdl ... if you are unsure, showing at least little bit of your code dealing with window would help.
JohnyDog at 2007-11-9 1:09:32 >
# 4 Re: how to catch close signal ?
I run on Windows system, and I create my application as windows console application ( don't use any gui api ). I run this app via command line e.g. C:\test.exe, and while it's running, user terminate my app via 'x' system menu. So that, windows console must be closed, and I want to do something before my app terminated, but I dont' know how to deal with this situation.

please advice
Butterfly at 2007-11-9 1:10:30 >
# 5 Re: how to catch close signal ?
from what i know, you dont.

Nothing gets sent to the app, it just gets terminated, as windows is emulating a dos session. Clicking on the x end the running of cmd.exe and not the program your running through cmd.exe. So in short, you cant handle for it.

I could be wrong.

Nik
KillerSponge at 2007-11-9 1:11:40 >
# 6 Re: how to catch close signal ?
Have a look at SetConsoleCtrlHandler. You just need a handler routine that responds to CTRL_CLOSE_EVENT.
TomWidmer at 2007-11-9 1:12:33 >
# 7 Re: how to catch close signal ?
thanks all,
thanks TomWidmer, that function can solve my problem ;)
Butterfly at 2007-11-9 1:13:37 >