Reputation: 135
Im trying to use the GetTickCount() in Windows API to get the system uptime. I want to know how long the system has been running.
However the return from GetTickCount is insanely high. This code gives me uptime of over 500 hours.
This goes for GetTickCount64() as well.
Why is this value so high?
DWORD time_ms = GetTickCount();
DWORD seconds = (time_ms / 1000) % 60;
DWORD minutes = time_ms /(1000*60) % 60;
DWORD hours = time_ms / (1000*60*60);
stringstream ss;
ss << "Hours: ";
ss << hours;
ss << " Minutes: ";
ss << minutes;
ss << " seconds: ";
ss << seconds;
MessageBoxA(0, ss.str().c_str(), "Uptime", 0);
As i run the program I can see that it is progressing correctly, but cannot comprehend how I will get the total uptime for my desktop.
Thanks
Edit: I checked the uptime with "systeminfo" in CMD and found that the "System boot time" was actually aprox ~500hours ago. So I shut down the computer and unplugged the electricity, booted but still,the System boot time had this high value. However, restarting the computer made it reset, and now my code works.
EDIT 2: This blog https://blogs.msdn.microsoft.com/oldnewthing/20141113-00/?p=43623 states that GetTickCount should rather be used for measuring time intervals than what I'm trying to achieve. Seems like I have to look at the registry.
EDIT 3:
After finding the right counter in the registry, it has the same value has GetTickCount and similar functions. It seems that shut down in Windows brings it to some sort of hibernation. I have not found any solution to this.
Upvotes: 4
Views: 6940
Reputation: 595742
Im trying to use the GetTickCount() in Windows API to get the system uptime. I want to know how long the system has been running.
In addition to what others have said, there is another alternative solution.
You can call NtQuerySystemInformation()
from ntdll.dll
, setting its SystemInformationClass
parameter to SystemTimeOfDayInformation
to retrieve a SYSTEM_TIMEOFDAY_INFORMATION
structure containing BootTime
and CurrentTime
fields as LARGE_INTEGER
values:
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;
You can simply subtract BootTime
from CurrentTime
to get the elapsed time that Windows has been running (this is exactly how Task Manager calculates the "System Up Time").
How does Task Manager compute Up Time, and why doesn’t it agree with GetTickCount?
Upvotes: 1
Reputation: 31
I just had the same problem. I solved it by :
I guess that a lot of old programs will bug again with W10 and the fast startup option...
Upvotes: 3
Reputation: 37122
The documentation for GetTickCount
describes the correct way to get the system up-time:
To obtain the time elapsed since the computer was started, retrieve the System Up Time counter in the performance data in the registry key HKEY_PERFORMANCE_DATA. The value returned is an 8-byte value. For more information, see Performance Counters
Upvotes: 5
Reputation: 612854
500 hours uptime is not especially surprising. That's around 20 days. Modern systems are seldom shutdown. Typically they are suspended rather than shutdown. I think your system really has been up for 500 hours.
Of course, you should be using GetTickCount64
to work with a 64 bit tick count. That will avoid the 49 day wrap around that is a consequence of the 32 bit values returned by GetTickCount
.
Upvotes: 1