Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetTickCount values on Windows 10

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.

like image 926
Gurra Avatar asked Jan 06 '23 16:01

Gurra


2 Answers

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

like image 170
Jonathan Potter Avatar answered Jan 14 '23 19:01

Jonathan Potter


I just had the same problem. I solved it by :

  • using the restart command rather that shutting down the computer : this performs a true restart of the computer and not a "half-hibernation" like the shutdown command when the "fast startup" is enabled
  • disabling the "fast startup" option. This fast startup option leads to a non reseted GetTickCount at startup !!

I guess that a lot of old programs will bug again with W10 and the fast startup option...

like image 44
Phuphus Avatar answered Jan 14 '23 17:01

Phuphus