由于是QT环境下,如果切换到VS中可将Q_OS_WIN 换为 #ifdef WIN32
#ifdef Q_OS_WIN
// windows header
#include "pdh.h"
#pragma comment(lib,"pdh.lib")
#endif
double Utils_GetCPUOverLoad()
{
#ifdef Q_OS_WIN
static PDH_HQUERY cpuQuery;
static PDH_HCOUNTER cpuTotal;
static bool isInit = false;
if (!isInit)
{
PdhOpenQuery(NULL, NULL, &cpuQuery);
// You can also use L"\\Processor(*)\\% Processor Time" and get individual CPU values with PdhGetFormattedCounterArray()
PdhAddEnglishCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PdhCollectQueryData(cpuQuery);
isInit = true;
}
PDH_FMT_COUNTERVALUE counterVal;
PdhCollectQueryData(cpuQuery);
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
return counterVal.doubleValue;
#endif
}
文章介绍了如何在从QT环境切换到VS时,利用PDH库在Windows系统中获取CPU总使用率的方法,包括初始化查询、收集数据和格式化计数器值。

2548

被折叠的 条评论
为什么被折叠?



