CPU (Windows): use PDH to detect thermal temp

No Administrator privileges needed
This commit is contained in:
李通洲
2025-04-12 10:47:57 +08:00
parent 090529ffef
commit 73bc71e922
3 changed files with 42 additions and 38 deletions
+1 -1
View File
@@ -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
+41 -3
View File
@@ -4,6 +4,46 @@
#include "util/mallocHelper.h"
#include "util/smbiosHelper.h"
#include <pdh.h>
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;
}
-34
View File
@@ -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<int32_t>() / 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<int32_t>() / 10 - 273.15; // In tenth of degrees Kelvin
else
*critical = 0.0/0.0;
}
return NULL;
}
return "No WMI result returned";
}