Current Situation:
I'm looking to try and measure the current CPU utilisation of my system in Hertz.
I've had a look at this answer which addresses my question, however I cannot seem to make the code work.
This is my current code in main.cpp (from the answer):
#include <Pdh.h>
#include <PdhMsg.h>
#include <Windows.h>
static PDH_HQUERY cpuQuery;
static PDH_HCOUNTER cpuTotal;
void init()
{
PDH_STATUS a = PdhOpenQuery(NULL, NULL, &cpuQuery);
PDH_STATUS i = PdhAddCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
PdhCollectQueryData(cpuQuery);
}
double getCurrentValue()
{
init();
PDH_FMT_COUNTERVALUE counterVal;
PdhCollectQueryData(cpuQuery);
PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
return counterVal.doubleValue;
}
int main()
{
double CPUUsage = getCurrentValue();
}
The Problem:
The value returned from getCurrectValue() is zero.
Related Observations:
I have observed, that the values in a and i of type PDH_STATUS are both zero? I speculate that this could be related to my lack of a value in CPUUsage, although I'm not sure why the function wouldn't be returning properly into these values.
Additional information:
I haven't used PDH before.