tags:

views:

127

answers:

1

What is the win32 function to check whether a shutdown is initiated or not?

EDIT: I need to check that inside a windows service (COM). How to do that?

+6  A: 

There's no actual Win32 function to check for that.

Instead Windows sends the WM_QUERYENDSESSION message to every application when a shutdown is initiated.

You can respond to that message and for example cancel the shutdown. (Although you shouldn't do that unless it is absolutely necessary)

Before the actual shutdown the WM_ENDSESSION message is sent.

You should do any of your cleanup only after this message, because it is not guaranteed that the system actually shuts down after WM_QUERYENDSESSION.

EDIT: If you want to listen for these messages from a Service you have to put some more work into it.

Services normally don't have windows, so you cannot simply hook into an existing window message queue. Instead you have to create a dummy window, which is meant only to processes messages and use it to handle the messages above.

See the MSDN documentation for more information about message-only windows.

DR
How can I check that inside a windows service (COM)?
iJeeves
You have to create a dummy window for that and check for these messages in its WindowProc.
DR
Thanks a lot for the wonderful explanation!
iJeeves