views:

604

answers:

1

I know that when Windows is shutting down, it sends a WM_QUERYENDSESSION message to each application. This makes it easy to detect when Windows is shutting down. However, is it possible to know if the computer going to power-off or is it going to restart after Windows has shutdown.

I am not particularly hopeful, considering the documentation at MSDN has this to say about WM_QUERYENDSESSION: "...it is not possible to determine which event is occurring," but the cumulative cleverness of stackoverflow never ceases to amaze me.

+6  A: 

From here:

You can read the DWORD value from "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shutdown Setting" to determine what the user last selected from the Shut Down dialog.

A bit of a roundabout solution, but it should do the trick.

Eric Petroelje
That is of course assuming that the system is now shutting down because the current user initiated it, and did so in Explorer. If this is a programmatic shutdown, shutdown from another app, or shutdown by another user, you'd get the reason for a previous shutdown.
MSalters