diff --git a/CMakeLists.txt b/CMakeLists.txt index 765bdc552..481fd855d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -977,7 +977,6 @@ elseif(WIN32) src/detection/btrfs/btrfs_nosupport.c src/detection/chassis/chassis_windows.c src/detection/cpu/cpu_windows.c - src/detection/cpu/cpu_windows.cpp src/detection/cpucache/cpucache_windows.c src/detection/cpuusage/cpuusage_windows.c src/detection/cursor/cursor_windows.c @@ -1590,6 +1589,7 @@ elseif(WIN32) PRIVATE "winbrand" PRIVATE "propsys" PRIVATE "secur32" + PRIVATE "pdh" ) elseif(FreeBSD) target_link_libraries(libfastfetch diff --git a/src/detection/cpu/cpu_windows.c b/src/detection/cpu/cpu_windows.c index 32c31768d..2f6a703b4 100644 --- a/src/detection/cpu/cpu_windows.c +++ b/src/detection/cpu/cpu_windows.c @@ -4,6 +4,46 @@ #include "util/mallocHelper.h" #include "util/smbiosHelper.h" +#include + +static void ffPdhOpenCloseQuery(HQUERY* query) +{ + assert(query); + if (*query) + { + PdhCloseQuery(*query); + *query = NULL; + } +} + +static const char* detectThermalTemp(double* result) +{ + // typeperf.exe -sc 1 "\Thermal Zone Information(*)\Temperature" + + __attribute__((__cleanup__(ffPdhOpenCloseQuery))) HQUERY query = NULL; + + if (PdhOpenQuery(NULL, 0, &query) != ERROR_SUCCESS) + return "Failed to open PDH query"; + + HCOUNTER counter = NULL; + if (PdhAddEnglishCounter(query, + L"\\Thermal Zone Information(*)\\Temperature", + 0, + &counter) != ERROR_SUCCESS) + return "Failed to add TZI temperature counter"; + + if (PdhCollectQueryData(query) != ERROR_SUCCESS) + return "Failed to collect query data"; + + PDH_FMT_COUNTERVALUE value; + if (PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, NULL, &value) != ERROR_SUCCESS) + return "Failed to format counter value"; + + *result = value.doubleValue - 273; + + return NULL; +} + // 7.5 typedef struct FFSmbiosProcessorInfo { @@ -165,8 +205,6 @@ static const char* detectCoreTypes(FFCPUResult* cpu) return NULL; } -const char* detectThermalTemp(double* current, double* critical); - const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) { detectNCores(cpu); @@ -182,7 +220,7 @@ const char* ffDetectCPUImpl(const FFCPUOptions* options, FFCPUResult* cpu) detectMaxSpeedBySmbios(cpu); if(options->temp) - detectThermalTemp(&cpu->temperature, NULL); + detectThermalTemp(&cpu->temperature); return NULL; } diff --git a/src/detection/cpu/cpu_windows.cpp b/src/detection/cpu/cpu_windows.cpp deleted file mode 100644 index 85b3ee084..000000000 --- a/src/detection/cpu/cpu_windows.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "util/windows/wmi.hpp" - -extern "C" -const char* detectThermalTemp(double* current, double* critical) -{ - // Requires Administrator privileges - // https://wutils.com/wmi/root/wmi/msacpi_thermalzonetemperature/#properties - FFWmiQuery query(L"SELECT CurrentTemperature, CriticalTripPoint FROM MSAcpi_ThermalZoneTemperature WHERE Active = TRUE", nullptr, FFWmiNamespace::WMI); - if(!query) - return "Query WMI service failed"; - - if(FFWmiRecord record = query.next()) - { - if (current) - { - if(auto vtCurrent = record.get(L"CurrentTemperature")) - *current = vtCurrent.get() / 10 - 273.15; // In tenth of degrees Kelvin - else - *current = 0.0/0.0; - } - - if (critical) - { - if(auto vtCritical = record.get(L"CriticalTripPoint")) - *critical = vtCritical.get() / 10 - 273.15; // In tenth of degrees Kelvin - else - *critical = 0.0/0.0; - } - - return NULL; - } - - return "No WMI result returned"; -}