Get CPU Temperature

32,677

Solution 1

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.

Solution 2

The link to source for OpenHardwareMonitorLib provided in Tomer's answer illustrates what has to happen at a low level to read this information out of different types of CPUs. For example, the IntelCPU class defines some model-specific registers:

private const uint IA32_THERM_STATUS_MSR = 0x019C;
private const uint IA32_TEMPERATURE_TARGET = 0x01A2;
private const uint IA32_PERF_STATUS = 0x0198;
private const uint MSR_PLATFORM_INFO = 0xCE;
private const uint IA32_PACKAGE_THERM_STATUS = 0x1B1;
private const uint MSR_RAPL_POWER_UNIT = 0x606;
private const uint MSR_PKG_ENERY_STATUS = 0x611;
private const uint MSR_DRAM_ENERGY_STATUS = 0x619;
private const uint MSR_PP0_ENERY_STATUS = 0x639;
private const uint MSR_PP1_ENERY_STATUS = 0x641;

These are straight out of Intel's docs like CPU Monitoring with DTS/PECI (section 16 "Direct MSR Access"). They may also be documented in the Intel Software Developer Manual but I haven't checked.

The OpenHardwareMonitorLib uses Rdmsr and RdmsrTx to get temperature values out of the MSRs of interest.

The corresponding AMD code looks like it gets similar info out of a PCI register. AMD will have equivalent documentation somewhere that will define this.

In both cases these are, by definition, how the hardware exposes information about its temperature sensors. You can use a library like this and it'll be doing this under the hood, or you can write equivalent code of your own.

Share:
32,677
Johnny Mnemonic
Author by

Johnny Mnemonic

Computer scientist

Updated on March 06, 2021

Comments

  • Johnny Mnemonic
    Johnny Mnemonic about 3 years

    I want to get the CPU temperature. Below is what I've done using C++ and WMI. I'm reading MSAcpi_ThermalZoneTemperature, but it's always the same and it's not the CPU temperature at all.

    Is there any way to get the real temperature of the CPU without having to write drivers? Or are there any libs which I can use? Thank you in advance.

    #define _WIN32_DCOM
    #include <iostream>
    using namespace std;
    #include <comdef.h>
    #include <Wbemidl.h>
    
    #pragma comment(lib, "wbemuuid.lib")
    
    HRESULT GetCpuTemperature(LPLONG pTemperature)
    {
            if (pTemperature == NULL)
                    return E_INVALIDARG;
    
            *pTemperature = -1;
            HRESULT ci = CoInitialize(NULL);
            HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
            if (SUCCEEDED(hr))
            {
                    IWbemLocator *pLocator;
                    hr = CoCreateInstance(CLSID_WbemAdministrativeLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLocator);
                    if (SUCCEEDED(hr))
                    {
                            IWbemServices *pServices;
                            BSTR ns = SysAllocString(L"root\\WMI");
                            hr = pLocator->ConnectServer(ns, NULL, NULL, NULL, 0, NULL, NULL, &pServices);
                            pLocator->Release();
                            SysFreeString(ns);
                            if (SUCCEEDED(hr))
                            {
                                    BSTR query = SysAllocString(L"SELECT * FROM MSAcpi_ThermalZoneTemperature");
                                    BSTR wql = SysAllocString(L"WQL");
                                    IEnumWbemClassObject *pEnum;
                                    hr = pServices->ExecQuery(wql, query, WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY, NULL, &pEnum);
                                    SysFreeString(wql);
                                    SysFreeString(query);
                                    pServices->Release();
                                    if (SUCCEEDED(hr))
                                    {
                                            IWbemClassObject *pObject;
                                            ULONG returned;
                                            hr = pEnum->Next(WBEM_INFINITE, 1, &pObject, &returned);
                                            pEnum->Release();
                                            if (SUCCEEDED(hr))
                                            {
                                                    BSTR temp = SysAllocString(L"CurrentTemperature");
                                                    VARIANT v;
                                                    VariantInit(&v);
                                                    hr = pObject->Get(temp, 0, &v, NULL, NULL);
                                                    pObject->Release();
                                                    SysFreeString(temp);
                                                    if (SUCCEEDED(hr))
                                                    {
                                                            *pTemperature = V_I4(&v);
                                                    }
                                                    VariantClear(&v);
                                            }
                                    }
                            }
                            if (ci == S_OK)
                            {
                                    CoUninitialize();
                            }
                    }
            }
            return hr;
    }
    
    int main(int argc, char **argv)
    {
            LONG temp;
            GetCpuTemperature(&temp);
            printf("temp=%lf\n", ((double)temp / 10 - 273.15));
            getc(stdin);
            return 0;
    }
    
  • cf-
    cf- about 10 years
    From those docs, Real-time readings for the CurrentReading property cannot be extracted from SMBIOS tables. For this reason, current implementations of WMI do not populate the CurrentReading property. Since op is looking for the current CPU temperature, I believe a working CurrentReading property would be necessary for this WMI class to do the job.
  • Johnny Mnemonic
    Johnny Mnemonic about 10 years
    Is that working for you Monster? It doesn't work for me and as computerfreaker said, according to the documentation CurrentReading isn't populated.
  • Monster
    Monster about 10 years
    Yes, it works. Anyway, it's implemented in class CIM_TemperatureSensor too, try it.
  • c00000fd
    c00000fd about 7 years
    I'm getting 0x80041010 error when I call IEnumWbemClassObject::Next(). Did anyone make any of his suggestions work?
  • josh poley
    josh poley almost 5 years
    Note that rdmsr requires a kernel driver, that can't be done from user mode code.