我正在尝试使用 Windows API 中的 GetTickCount() 来获取系统正常运行时间。我想知道系统运行了多长时间。
但是 GetTickCount 的返回高得离谱。此代码使我的正常运行时间超过 500 小时。
这也适用于 GetTickCount64()。
为什么这个值这么高?
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);
当我运行该程序时,我可以看到它正在正确进行,但无法理解我将如何获得桌面的总正常运行时间。
谢谢
编辑: 我在 CMD 中使用“systeminfo”检查了正常运行时间,发现“系统启动时间”实际上是大约 500 小时前。所以我关闭电脑并拔掉电源,启动但系统启动时间仍然有这么高的值。但是,重新启动计算机使其重置,现在我的代码可以工作了。
编辑 2: 本博https://blogs.msdn.microsoft.com/oldnewthing/20141113-00/?p=43623声明 GetTickCount 应该用于测量时间间隔而不是我想要实现的目标。好像我必须看看注册表。
编辑 3:
在注册表中找到正确的计数器后,它具有相同的值,具有 GetTickCount 和类似的功能。似乎在 Windows 中关闭会使它进入某种休眠状态。我还没有找到任何解决方案。
最佳答案
GetTickCount
的文档描述了获得系统正常运行时间的正确方法:
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
关于c++ - Windows 10 上的 GetTickCount 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35997227/