How can I get Windows boot time in C++ with some WinAPI functions? I am currently using the command "systeminfo", but I'm looking for a more proper solution.
Asked
Active
Viewed 3,205 times
3
-
Should just be System::Environment::TickCount, right? – BonzaiThePenguin Sep 27 '14 at 12:05
-
1"Proper" code would use WMI to retrieve info like this. Win32_OperatingSystem class, LastBootUpTime property. It does however have the tendency to no longer be considered "proper" when a C++ programmer discovers what it takes to write WMI queries. Or really all that necessary :) – Hans Passant Sep 27 '14 at 12:06
-
Related: http://stackoverflow.com/questions/2453191/how-get-win32-operatingsystem-lastbootuptime-in-datetime-format – David Heffernan Sep 27 '14 at 12:41
-
Could you enlighten me what's wrong with this question, that it gets downvotes? – Paul Sep 27 '14 at 13:42
1 Answers
4
I've checked at how Task Manager does it, and turns out it uses the ZwQuerySystemInformation(SystemTimeOfDayInformation, ...)
function, which receives the SYSTEM_TIMEOFDAY_INFORMATION
structure.
typedef struct _SYSTEM_TIMEOFDAY_INFORMATION {
LARGE_INTEGER BootTime;
LARGE_INTEGER CurrentTime;
LARGE_INTEGER TimeZoneBias;
ULONG TimeZoneId;
ULONG Reserved;
ULONGLONG BootTimeBias;
ULONGLONG SleepTimeBias;
} SYSTEM_TIMEOFDAY_INFORMATION, *PSYSTEM_TIMEOFDAY_INFORMATION;
BootTime
is what you're looking for.
For something more documented, I think GetTickCount64
will do.

Paul
- 6,061
- 6
- 39
- 70
-
That seems pretty unlikely to be true. How would the value returned be simultaneously be used for generating an unpredictable seed for a random number generator, and for reading the system boot time? – David Heffernan Sep 27 '14 at 12:12
-
@DavidHeffernan, just Googled [this](http://read.pudn.com/downloads136/sourcecode/windows/network/578829/snmp/common/dll/uptime.c__.htm). – Paul Sep 27 '14 at 12:13
-
That is code to measure elapsed time. The question asks for the time taken for the system to boot. Which is different. – David Heffernan Sep 27 '14 at 12:17
-
Or perhaps what is asked for is the time at which the system started. Either way, your answer does not address that. – David Heffernan Sep 27 '14 at 12:28
-
I think it's the latter. What I'm talking about is [this](http://i.imgur.com/WwJa7C3.png). – Paul Sep 27 '14 at 12:29
-
As I said, either way, your answer doesn't address the question. – David Heffernan Sep 27 '14 at 12:31
-
Why not? It provides you with the system boot time. Updated answer with more information. Task manager calculates the Up Time by doing `CurrentTime-BootTime`. – Paul Sep 27 '14 at 12:37
-
OK, that's more like it. Now you've given some more useful information. However, this is undocumented. We've no real idea whether or not the struct really contains those members. In all versions of windows. Even future ones. So, I certainly would not attempt to abuse this function for something that it was not intended for. – David Heffernan Sep 27 '14 at 12:39
-
-
-
"The elapsed time retrieved by `GetTickCount` or `GetTickCount64` includes time the system spends in sleep or hibernation." [(source)](http://msdn.microsoft.com/en-us/library/windows/desktop/ms725496.aspx) – Paul Sep 27 '14 at 12:44
-
See a blog post on [The Old New Thing](http://blogs.msdn.com/b/oldnewthing/archive/2015/05/12/10614015.aspx) – Andomar May 20 '15 at 15:31
-
1Note that `ZwQuerySystemInformation` [is no longer available for use as of Windows 8](https://msdn.microsoft.com/en-us/library/windows/desktop/ms725506(v=vs.85).aspx); moreover, the suggested alternative on MSDN for `SystemTimeOfDayInformation` is `CryptGenRandom`... which does not help in getting the boot time. – Andy Apr 11 '18 at 19:30
-
@Andy it's still available on my Windows 10. What they meant is probably that it's no longer supported. – Paul Apr 11 '18 at 20:37
-
@Paul SystemTimeOfDayInformation returns random garbage: "Returns an opaque SYSTEM_TIMEOFDAY_INFORMATION structure that can be used to generate an unpredictable seed for a random number generator. Use the CryptGenRandom function instead". – Alex Apr 09 '20 at 16:27
-
@Alex the fact that it can be used as a seed doesn't mean it's random garbage. In C, you use `srand(time(NULL))`, but `time(NULL)` doesn't return garbage. And in any case, that's the call Task Manager uses for displaying the boot time information. So it must be working, unless you see random garbage in your Task Manager. – Paul Apr 09 '20 at 19:37
-
@Paul Well, I though it was changed in some version of Windows, since it was undocumented (Task Manager could be rewritten as well). However, now I've checked that it still returns boot time. – Alex Apr 10 '20 at 09:45