Get CPU Temperature

Truth to be told, it depends on the hardware.

A library that works on most hardware is OpenHardwareMonitorLib. Unfortunately, it has no documentation and actually doesn’t really exist as an independent piece of software. It’s part of an open source software named “Open Hardware Monitor”. it’s done in .NET C Sharp and of course, only works for Windows. Fortunately you can get it as a DLL, and the GUI is completely separated from the actual backend which is OpenHardwareMonitorLib.

Read this post on how to use it from C++

How to call a C# library from Native C++ (using C++\CLI and IJW)

So considering it has no docs it can be a bit hard to work with. After reading the source for a while this is my take:

using OpenHardwareMonitor.Hardware;
...
float? cpu_temperature_celcius = null;
Computer computer = new Computer();
computer.CPUEnabled = true;
computer.Open();
foreach (IHardware hardware in computer.Hardware)
    if (hardware.HardwareType == HardwareType.CPU)
        foreach (ISensor sensor in hardware.Sensors)
            if (sensor.SensorType == SensorType.Temperature)
                cpu_temperature_celcius = sensor.Value;

This C# code gets the temperature of the CPU in Celcius. Tested on an Intel Haswell CPU. It will most likely work for most other CPUs from AMD and Intel. OpenHardwareMonitorLib.dll is needed. You can compile it from source

You can get a lot of other information about the system with this library.

Note that the CPU of the user can have multiple temperature sensors. For example, a temperature sensor for every core, so don’t always take the last one as I did in the example above.

Good luck.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)