80

I'd like to know what is the Windows API function (if any exists) that provides information about the last Windows reboot source. There are three main possible causes:

  1. The computer crashed on a blue screen
  2. A user or a program shutdown/restarted the computer
  3. A power lost

The more details I can get the better. However, I need to know at least which reason it is from the main ones.

I need to support Windows Vista and Windows 7.

Answer:

It seems that there is no direct API to get that information. Instead, we have to harvest the Windows Event Log. System reboot information is located in Event Viewer/Windows Logs/System. Here is the various information provided by the event ids:

  • 6005: Windows start-up
  • 6006: Windows shutdown (properly)
  • 6008: Windows shutdown (unexpectedly)

I do not yet get the difference between power lost and system crash, but it's a good start.

M. Tibbits
  • 8,075
  • 7
  • 40
  • 58
Frederic
  • 1,805
  • 2
  • 17
  • 22

2 Answers2

88

This article explains in detail how to find the reason for last startup/shutdown. In my case, this was due to windows SCCM pushing updates even though I had it disabled locally. Visit the article for full details with pictures. For reference, here are the steps copy/pasted from the website:

  1. Press the Windows + R keys to open the Run dialog, type eventvwr.msc, and press Enter.

  2. If prompted by UAC, then click/tap on Yes (Windows 7/8) or Continue (Vista).

  3. In the left pane of Event Viewer, double click/tap on Windows Logs to expand it, click on System to select it, then right click on System, and click/tap on Filter Current Log.

  4. Do either step 5 or 6 below for what shutdown events you would like to see.

  5. To see the dates and times of all user shut downs of the computer

A) In Event sources, click/tap on the drop down arrow and check the USER32 box.

B) In the All Event IDs field, type 1074, then click/tap on OK.

C) This will give you a list of power off (shutdown) and restart shutdown type of events at the top of the middle pane in Event Viewer.

D) You can scroll through these listed events to find the events with power off as the shutdown type. You will notice the date and time, and what user was responsible for shutting down the computer per power off event listed.

E) Go to step 7.

  1. To see the dates and times of all unexpected shut downs of the computer

A) In the All Event IDs field type 6008, then click/tap on OK.

B) This will give you a list of unexpected shutdown events at the top of the middle pane in Event Viewer. You can scroll through these listed events to see the date and time of each one.

Andreas
  • 4,832
  • 6
  • 40
  • 49
Mahesh Neelakanta
  • 1,132
  • 1
  • 9
  • 4
  • Perfect. I used the Event Source filter with USER32 and found out that Dell recovery was rebooting my system. – max Dec 09 '15 at 18:23
  • Thanks! Filtering System Events with event IDs of **6008**, lists the critical events that caused shutdown. – Noam Manos Feb 02 '16 at 14:16
  • This is essentially a link-only answer. In case the link becomes inaccessible, this answer becomes devoid of any useful information. Besides, the OP is asking for an API call. Your link provides the answer by navigating the user through a series of manual steps. This does not constitute a solution to the problem asked. – IInspectable Jul 26 '17 at 12:40
  • It is weird.. It is telling me Administrator is calling shutdown. It has been happening more frequently recently and noticing that even more than once several minutes after each other. I must have been compromised :( – Piotr Kula Nov 23 '17 at 09:19
  • If the system rebooted due to a bugcheck/BSOD, the event ID will be 1001, not 6008. [This link contains explanations of the different bugcheck codes you might find in the Event Viewer](https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/bug-check-code-reference2). – aviraldg Nov 14 '20 at 22:02
17

Take a look at the Event Log API. Case a) (bluescreen, user cut the power cord or system hang) causes a note ('system did not shutdown correctly' or something like that) to be left in the 'System' event log the next time the system is rebooted properly. You should be able to access it programmatically using the above API (honestly, I've never used it but it should work).

Ted Bigham
  • 3,872
  • 1
  • 21
  • 29
Alexander Gessler
  • 42,787
  • 5
  • 78
  • 120
  • 2
    Found it, it's in the System event log. – Frederic Jan 27 '10 at 17:40
  • 3
    I've noticed that this message appears when the system freezes: "The system has rebooted without cleanly shutting down first. This error could be caused if the system stopped responding, crashed, or lost power unexpectedly." This seems like a generic message. Does anyone know if there's a way to differentiate between bluescreens and system freezes? – Luiz C. Oct 16 '11 at 22:49