how to link the windows shutting and program

hi,
i want my program to sthut down when the windows shuts down. It is dialog based applicatoin.
How to do it?
regards
Anima
[149 byte] By [anima_sg] at [2007-11-18 1:38:20]
# 1 Re: how to link the windows shutting and program
All programs are automatically sent a notification message when Windows is shutting down. There should be no need for additional programming in a standard application that responds to WM_CLOSE.
TheCPUWizard at 2007-11-10 8:53:59 >
# 2 Re: how to link the windows shutting and program
If your program dont have Visible Window and Dont handle WM_CLOSE message , then the prgram will be terminated without notification , I think OS dont use SIGTERM too.
Krishnaa at 2007-11-10 8:54:59 >
# 3 Re: how to link the windows shutting and program
i want to do some cleaning when the program closes but since my exe runs in background there is no gui to shutdow nthe system.
Which msg should i trap?? in order that anyhow the system shuts down i get the msg
anima_sg at 2007-11-10 8:56:08 >
# 4 Re: how to link the windows shutting and program
Hello Guys!
Check this out its form MSDN :

WM_QUERYENDSESSION:

The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls the ExitWindows function. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.

After processing this message, the system sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message.

A window receives this message through its WindowProc function.

LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
UINT uMsg, // WM_QUERYENDSESSION
WPARAM wParam, // not used
LPARAM lParam // logoff option
);

Parameters:

wParam :
This parameter is reserved for future use.

lParam :
Specifies whether the user is logging off or shutting down the system. If this parameter includes the ENDSESSION_LOGOFF value, the user if logging off. (Note that this parameter is a bit mask. To test for this value, use a bitwise operation; do not test for equality.)

Windows 2000:
If this parameter is zero, the system is shutting down.

Return Values:
If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE.

Remarks:
By default, the DefWindowProc function returns TRUE for this message.

Windows NT/2000:
When an application returns TRUE for this message, it receives the WM_ENDSESSION message and it is terminated, regardless of how the other applications respond to the WM_QUERYENDSESSION message.

Windows 95/98:
After all applications return TRUE for this message, they receive the WM_ENDSESSION and they are terminated.

Hope this can help :)

Aamir!
aamirnadil at 2007-11-10 8:57:01 >
# 5 Re: how to link the windows shutting and program
but this msg is only send when the user wants to shut down or application calls exit function in my case there is not GUI and the applciation is not calling exitfunctio nto close.........to now how do i trap it
anima_sg at 2007-11-10 8:58:10 >
# 6 Re: how to link the windows shutting and program
anima_sg!

U need no GUI to receive this message, what u need is a "WndProc" in ur exe (or a message handler in MFC) so that it can wrestle with this message. This message is broadcasted when windows is shutting down or user tries to logoff. If u have difficulty using it, i'll try to send a sample to u.

Aamir!
aamirnadil at 2007-11-10 8:59:09 >
# 7 Re: how to link the windows shutting and program
pz send an application ............
anima_sg at 2007-11-10 9:00:12 >
# 8 Re: how to link the windows shutting and program
Just create a hidden window and process the messages.
Krishnaa at 2007-11-10 9:01:15 >
# 9 Re: how to link the windows shutting and program
Hello anima_sg!

Here is a sample application but it is is Win32, i hope u can convert it into MFC else i'll try to do it for u :)

I was having some difficulty with MessageBox so i created my own dialog for this purpose, this program will not allow to shutdown or logoff and neither it will close by itself, u'll have to kill it through Task Manager.

Any difficulty, ur free to ask.

Aamir!
aamirnadil at 2007-11-10 9:02:12 >