From 32d3edc69012ea5de6f355361ae01150307017e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 7 Oct 2022 15:57:09 +0800 Subject: [PATCH] CPU: add support for Windows --- CMakeLists.txt | 2 +- src/detection/cpu/cpu_windows.cpp | 51 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/detection/cpu/cpu_windows.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b0060dff..2a4bec5c4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -314,6 +314,7 @@ if(WIN_MSYS) src/detection/host/host_windows.cpp src/detection/bios/bios_windows.cpp src/detection/board/board_windows.cpp + src/detection/cpu/cpu_windows.cpp src/detection/gpu/gpu_windows.cpp src/detection/battery/battery_windows.cpp src/detection/displayserver/displayserver_windows.c @@ -332,7 +333,6 @@ if(WIN_MSYS) # TODO src/detection/media/media_linux.c src/detection/font/font_linux.c - src/detection/cpu/cpu_linux.c src/detection/memory/memory_linux.c src/detection/cpuUsage/cpuUsage_linux.c src/detection/temps/temps_linux.c diff --git a/src/detection/cpu/cpu_windows.cpp b/src/detection/cpu/cpu_windows.cpp new file mode 100644 index 000000000..250212cfc --- /dev/null +++ b/src/detection/cpu/cpu_windows.cpp @@ -0,0 +1,51 @@ +extern "C" { +#include "cpu.h" +} +#include "util/windows/wmi.hpp" + +extern "C" +void ffDetectCPUImpl(const FFinstance* instance, FFCPUResult* cpu, bool cached) +{ + FF_UNUSED(instance); + + cpu->temperature = FF_CPU_TEMP_UNSET; + + if(cached) + return; + + cpu->coresPhysical = cpu->coresLogical = cpu->coresOnline = 0; + ffStrbufInit(&cpu->name); + ffStrbufInit(&cpu->vendor); + + IEnumWbemClassObject* pEnumerator = ffQueryWmi(L"SELECT Name, Manufacturer, NumberOfCores, NumberOfLogicalProcessors, ThreadCount, CurrentClockSpeed, MaxClockSpeed FROM Win32_Processor WHERE ProcessorType = 3", nullptr); + if(!pEnumerator) + return; + + IWbemClassObject *pclsObj = NULL; + ULONG uReturn = 0; + + if(FAILED(pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn)) || uReturn == 0) + { + pEnumerator->Release(); + return; + } + + ffGetWmiObjString(pclsObj, L"Name", &cpu->name); + ffGetWmiObjString(pclsObj, L"Manufacturer", &cpu->vendor); + + uint64_t value; + + ffGetWmiObjUnsigned(pclsObj, L"NumberOfCores", &value); + cpu->coresPhysical = (uint16_t)value; + ffGetWmiObjUnsigned(pclsObj, L"NumberOfLogicalProcessors", &value); + cpu->coresLogical = (uint16_t)value; + ffGetWmiObjUnsigned(pclsObj, L"ThreadCount", &value); + cpu->coresOnline = (uint16_t)value; + ffGetWmiObjUnsigned(pclsObj, L"CurrentClockSpeed", &value); //There's no MinClockSpeed in Win32_Processor + cpu->frequencyMin = (double)value / 1000.0; + ffGetWmiObjUnsigned(pclsObj, L"MaxClockSpeed", &value); + cpu->frequencyMax = (double)value / 1000.0; + + pclsObj->Release(); + pEnumerator->Release(); +}