我正在尝试使用 C++ 中的性能计数器读取系统正常运行时间。我想至少同时支持 XP 和 Windows 7。
以下代码在 Windows XP 上运行良好...
HQUERY hQuery; HCOUNTER hCounter;
PDH_FMT_COUNTERVALUE Value;
int ret = 0;
if (PdhOpenQuery(NULL, 0, &hQuery) == ERROR_SUCCESS) {
if ((status = PdhAddCounter(hQuery, queryURI, 0, &hCounter)) == ERROR_SUCCESS) {
if ((status = PdhCollectQueryData(hQuery)) == ERROR_SUCCESS) {
if ((status = PdhGetFormattedCounterValue(hCounter, PDH_FMT_LARGE, NULL, &Value)) == ERROR_SUCCESS) {
ret = (DWORD)(Value.largeValue);
}
}
PdhRemoveCounter(hCounter);
}
PdhCloseQuery(hQuery);
}
return ret;
..但它在 Windows 7 上失败。具体来说,无论我是否以管理员身份运行, PdhCollectQueryData 都会返回 PDH_NO_DATA。
如何在 Windows 7 和 XP 上获得系统正常运行时间?我希望时间比 GetTickCount 的 49 天溢出要大得多,如果可能的话,我宁愿没有单独的用于 XP 的 PDH 版本和用于 7 的 GetTickCount64 版本......