Windows 10 上的 GetTickCount 值

2022-01-15 00:00:00 windows windows-10 winapi c++

我正在尝试使用 Windows API 中的 GetTickCount() 来获取系统正常运行时间.我想知道系统运行了多长时间.

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.

但是,GetTickCount 的回报非常高.此代码使我的正常运行时间超过 500 小时.

However the return from GetTickCount is insanely high. This code gives me uptime of over 500 hours.

这也适用于 GetTickCount64().

This goes for GetTickCount64() as well.

为什么这个值这么高?

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.

谢谢

编辑:我在 CMD 中使用systeminfo"检查了正常运行时间,发现系统启动时间"实际上是大约 500 小时前.所以我关掉了电脑,拔掉了电源,启动了,但是系统启动时间还是有这么高的值.但是,重新启动计算机使其重置,现在我的代码可以工作了.

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.

编辑 2:此博客 https://blogs.msdn.microsoft.com/oldnewthing/20141113-00/?p=43623 指出 GetTickCount 应该用于测量时间间隔,而不是我想要实现的.看来我必须查看注册表.

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.

编辑 3:

在注册表中找到正确的计数器后,具有相同的值有GetTickCount 和类似的功能.似乎在 Windows 中关闭会使其进入某种休眠状态.我还没有找到任何解决方案.

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.

推荐答案

GetTickCount 描述了获取系统正常运行时间的正确方法:

The documentation for GetTickCount describes the correct way to get the system up-time:

要获取计算机启动后经过的时间,请检索注册表项中性能数据中的 System Up Time 计数器HKEY_PERFORMANCE_DATA.返回的值是一个 8 字节的值.更多信息,请参阅性能计数器

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

相关文章