由于是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
}