Skip to content

Instantly share code, notes, and snippets.

@quink-black
Created May 23, 2024 13:06
Show Gist options
  • Save quink-black/b771945321385cc15642543b3bd802bb to your computer and use it in GitHub Desktop.
Save quink-black/b771945321385cc15642543b3bd802bb to your computer and use it in GitHub Desktop.
Windows CPU frequency
#include <windows.h>
#include <ntstatus.h>
#include <powerbase.h>
#include <stdio.h>
#include <vector>
#pragma comment(lib, "powrprof.lib")
typedef struct _PROCESSOR_POWER_INFORMATION {
ULONG Number;
ULONG MaxMhz;
ULONG CurrentMhz;
ULONG MhzLimit;
ULONG MaxIdleState;
ULONG CurrentIdleState;
} PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;
int main(int argc, char *argv[]) {
SYSTEM_INFO sys_info{};
GetSystemInfo(&sys_info);
std::vector<PROCESSOR_POWER_INFORMATION> ppi(sys_info.dwNumberOfProcessors);
DWORD bufferSize = ppi.size() * sizeof(ppi[0]);
NTSTATUS status = CallNtPowerInformation(ProcessorInformation, NULL, 0, ppi.data(), bufferSize);
if (status != STATUS_SUCCESS) {
printf("Failed to get CPU frequency\n");
return EXIT_FAILURE;
}
int i = 0;
for (const auto &item: ppi) {
printf("CPU %d, max freq %ld mHz, cur freq %ld mHz, limit freq %ld mHz\n",
i, item.MaxMhz, item.CurrentMhz, item.MhzLimit);
i++;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment