Reputation: 1456
The Windows 8 Task manager shows the current (not maximum) frequency of the CPU (e.g. 1.2 GHz). Is there a way to get this frequency with the Windows API? Preferrably using Delphi or Visual C++.
Upvotes: 2
Views: 1080
Reputation: 163
https://stackoverflow.com/a/78403806/7609214
The frequency obtained by the following method is similar to the CPU speed in the Task Manager
#include <Pdh.h> // link to Pdh.lib
#define YOUR_CPU_MAX_FREQUENCY 3.3
HQUERY hquery;
// https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhopenquerya
PdhOpenQueryA(nullptr, NULL, &hquery)
HCOUNTER hcounter;
// https://learn.microsoft.com/en-us/windows/win32/api/pdh/nf-pdh-pdhaddcountera
PdhAddCounterA(hquery, "\\Processor Information(_Total)\\% Processor Performance", NULL, &hcounter)
PdhCollectQueryData(hquery);
Sleep(200);
PdhCollectQueryData(hquery);
PDH_FMT_COUNTERVALUE value;
PdhGetFormattedCounterValue(hcounter, PDH_FMT_DOUBLE, nullptr, &value);
PdhCloseQuery(hquery);
double frequency = value.doubleValue / 100 * YOUR_CPU_MAX_FREQUENCY;
frequency will be a value that changes in real time (in ghz), such as 1.2, 2.6, ...
Upvotes: 0