From b5cf22ff1e24715445eb92c6136ad99fdca6128f Mon Sep 17 00:00:00 2001 From: ColumbusTech Date: Thu, 22 Nov 2018 15:09:07 +0300 Subject: [PATCH] Windows --- corinfo.h | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/corinfo.h b/corinfo.h index bb1203d..540bc03 100644 --- a/corinfo.h +++ b/corinfo.h @@ -37,6 +37,8 @@ #ifdef _WIN32 #include + #include + #pragma comment(lib, "Powrprof.lib") #endif #include @@ -161,11 +163,29 @@ static void __extensions(int cpu[4], struct corinfo* info); SYSTEM_INFO sys; MEMORYSTATUSEX mem; + + struct + { + ULONG Number; + ULONG MaxMhz; + ULONG CurrentMhz; + ULONG MhzLimit; + ULONG MaxIdleState; + ULONG CurrentIdleState; + } PROCESSOR_POWER_INFORMATION, *PPI; + mem.dwLength = sizeof(mem); GetSystemInfo(&sys); GlobalMemoryStatusEx(&mem); + BYTE* buf = (BYTE*)malloc(sizeof(PROCESSOR_POWER_INFORMATION) * 4); + if (buf == NULL) return -1; + + CallNtPowerInformation(ProcessorInformation, NULL, 0, buf, sizeof(PROCESSOR_POWER_INFORMATION) * sys.dwNumberOfProcessors); + + PPI = buf; + int cpu[4]; // CPUID 0x00 code returns in EAX maximum CPUID code and vendor string in EBX, EDX, ECX. __cpuid(cpu, 0x00000000); @@ -173,31 +193,15 @@ static void __extensions(int cpu[4], struct corinfo* info); __brand_string(cpu, info->BrandString); __extensions(cpu, info); - // Some WinAPI hell. - wchar_t Buffer[_MAX_PATH]; - DWORD BufSize = _MAX_PATH; - DWORD dwMHz = _MAX_PATH; - HKEY hKey; - - long lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, - L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_READ, &hKey); - - if (lError != ERROR_SUCCESS) - { - FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, lError, 0, Buffer, _MAX_PATH, 0); - wprintf(Buffer); - return 0; - } - - RegQueryValueEx(hKey, L"~MHz", NULL, NULL, (LPBYTE)&dwMHz, &BufSize); - info->CpuCount = sys.dwNumberOfProcessors; - info->CpuFrequency = dwMHz; + info->CpuFrequency = PPI->MaxMhz; info->RamSize = mem.ullTotalPhys / 1024; info->RamFree = mem.ullAvailPhys / 1024; info->RamUsage = mem.dwMemoryLoad; + free(buf); + return 0; } #endif