在Windows/MSVC环境下使用lhwm-wrapper获取CPU温度的参数疑问
解决lhwm-wrapper获取CPU温度的标识符问题
1. 先调用GetHardwareSensorMap()获取所有传感器信息
这个函数返回的map中,key是传感器的唯一标识符,value是传感器的描述信息(包含硬件类型、传感器类型等)。你需要遍历这个map,筛选出属于CPU且类型为温度的条目,才能找到可用的标识符。
示例代码(MSVC环境下):
#include <iostream> #include <map> #include <string> // 确保已引入lhwm-wrapper的头文件并链接对应库 extern std::map<std::string, std::string> GetHardwareSensorMap(); extern float GetSensorValue(std::string identifier); int main() { // 获取全量传感器映射 auto sensorMap = GetHardwareSensorMap(); // 遍历筛选CPU温度传感器 for (const auto& pair : sensorMap) { std::string id = pair.first; std::string desc = pair.second; // 通过描述关键词筛选:通常包含"CPU"和"Temperature" if (desc.find("CPU") != std::string::npos && desc.find("Temperature") != std::string::npos) { std::cout << "标识符: " << id << " | 描述: " << desc << std::endl; // 同步测试获取当前温度值 float temp = GetSensorValue(id); std::cout << "当前温度: " << temp << "°C" << std::endl; } } return 0; }
2. 用正确标识符直接调用
运行上述代码后,控制台会输出所有CPU温度传感器的标识符和对应温度。比如你可能得到类似输出:
标识符: cpu/0/temperature/0 | 描述: CPU Core #0 Temperature
当前温度: 42.5°C
之后直接使用该标识符(如"cpu/0/temperature/0")调用GetSensorValue(),就能获取对应CPU核心的温度。
3. 为什么传"0"会返回0?
GetSensorValue()需要的是Libre Hardware Monitor定义的标准传感器标识符,并非简单数字索引。如果传入的标识符不存在,函数会默认返回0,这就是你之前测试失败的原因。
内容的提问来源于stack exchange,提问作者kaan kaya
相关产品推荐
相关产品推荐

