3

Is there a way to capture if Windows shutting down, or it is restarting? For shutdown, i found some solutions, but not for restart. For me it would be important to be able to determine if Windows is restarting, or just shutting down.

Thanks!

kampi
  • 2,362
  • 12
  • 52
  • 91
  • Take a look at http://stackoverflow.com/questions/981306/how-to-detect-whether-windows-is-shutting-down-or-restarting – Sean Dec 20 '13 at 11:47
  • @Sean: I found that, but my problem is with this solution, what is described there. What happenes for example if i make an appplication and it will restart Windows? Then the user didn't chose to restart the system, so i can't read the registry value. – kampi Dec 20 '13 at 11:52
  • 1
    OK. Why is it imporant to know the difference between a shutdown and a restart. After all, if the user shuts down and then powers back up straight away it's basically the same thing. – Sean Dec 20 '13 at 11:55
  • In my case unfortunately it is not. It is very complicated why, but i need to know the difference :) – kampi Dec 20 '13 at 11:59
  • possible duplicate of [How can I get the Windows last reboot reason](http://stackoverflow.com/questions/2140092/how-can-i-get-the-windows-last-reboot-reason) – MSalters Dec 20 '13 at 12:58
  • 2
    You **can't** know this, really. Windows **may** think it will restart, but it's not uncommon to turn a machine off when it's finished its shutdown in preparation for a restart. And obviously, since the machine at that point is shut down, you can't detect that. – MSalters Dec 20 '13 at 13:02
  • 1
    You get a *little* bit of info from the WM_QUERYENDSESSION notification. Windows strongly avoids telling you too much about the shutdown reason, it cannot guarantee that (say) a sleep or hibernate is going to allow the OS to restart from the saved state. It isn't around to ensure that a restart will actually happen. – Hans Passant Dec 20 '13 at 14:02

1 Answers1

0

There is a way to detect whether it is shutting down and also there is a way to detect when it starts up.

So, you can note the time when it is shutting down. Note the time when it is starting by running an exe at startup (add your exe here in registry: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce).

If the time difference is very less then you can treat it as a 'Restart'.

Also you can use WTSRegisterSessionNotification api for exact events.

Community
  • 1
  • 1
Gautam Jain
  • 6,789
  • 10
  • 48
  • 67