Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether a shutdown is initiated or not

Tags:

c++

winapi

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?

like image 253
softwarematter Avatar asked Aug 06 '09 11:08

softwarematter


1 Answers

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.

like image 190
Daniel Rikowski Avatar answered Oct 18 '22 12:10

Daniel Rikowski