Casady
Casady

Reputation: 1456

Find out the current frequency of the CPU

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

Answers (2)

Hongfei Shen
Hongfei Shen

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

ESG
ESG

Reputation: 9435

I would look into WMI, specifically, the CurrentClockSpeed property of the Win32_Processor class.

MSDN link

Upvotes: 5

Related Questions