c++windowscputemperatureopenhardwaremonitor

How to get CPU temperature with Open Hardware Monitor?


So I've been trying to get CPU temperature on windows, msvc, at last I found Libre Hardware Monitor, fork of Open Hardware Monitor.

And someone(Get CPU Temperature) said he used lhwm-wrapper(https://gitlab.com/OpenRGBDevelopers/lhwm-wrapper) so it can be used with c++.

There is three functions this lhwm-wrapper exports:

GetHardwareSensorMap()
//returns a map

GetSensorValue(std::string identifier)
//returns float

SetControlValue(std::string identifier, float value)
//void function

So I've been curious about how to use this functions to get CPU heat, I think I should use GetSensorValue(std::string identifier) to access but I don't know what to input. Maybe CPU's heat sensor's value I have completely no idea.

I created a visual studio project where I can use lhwm-wrapper, and tried to give some values to GetSensorValue(std::string identifier)

like this:

#include <iostream>
#include <lhwm-cpp-wrapper.h>

int main()
{
    std::cout << LHWM::GetSensorValue("0");
}

whatever I gave as input outputed as 0.


Solution

  • All the information you need is found in the map, in C++ this is a map of tuples, each holding three data points:

    1. Sensor Name
    2. Sensor Type
    3. Sensor ID (this is what you want)

    Each piece of hardware has various sensors of various types, each with their respective values. In your case, you want to find a sensor of type Temperature.

    You find all this information with the provided LHWM::GetHardwareSensorMap().

    For example. I found my CPU AMD Ryzen 7 PRO 4750U with Radeon Graphics, where within the many tuples I found a single sensor of type Temperature with the ID /amdcpu/0/temperature/2. So, I plugged that in as follows.

    std::string sensorId = "/amdcpu/0/temperature/2";
    float temp = LHWM::GetSensorValue(sensorId);
    std::cout << "Temperature: " << temp << std::endl;
    

    Which gave me the value 57.625 in degrees Celsius.

    Note: The program must be running with elevated privileges, otherwise you'll get a reading of 0. This is what happened to me and caused some amount of initial confusion.