'Get CPU Temperature
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;
}
Solution 1:[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:[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.
Solution 3:[3]
The mentioned WMI classes were not working for me in latest Windows 10. On my Dell-Laptop I could get the CPU-Temperature in Celsius here:
ROOT_CIMV2\Win32_PerfFormattedData_Counters_ThermalZoneInformation\HighPrecisionTemperature
Solution 4:[4]
WMI has the Win32_TemperatureProbe class:
http://msdn.microsoft.com/en-us/library/aa394493%28VS.85%29.aspx
Try it instead of MSAcpi_ThermalZoneTemperature
upd.
So, I tried the code from MS example page here. It shows the way to retrieve information from WMI classes.
It's generally the same as yours but the class name and property name. So change the line
BSTR query = SysAllocString(L"SELECT * FROM MSAcpi_ThermalZoneTemperature");
to
BSTR query = SysAllocString(L"SELECT * FROM Win32_TemperatureProbe");
or to it's parent class
BSTR query = SysAllocString(L"SELECT * FROM CIM_TemperatureSensor");
then change the property name to "CurrentReading"
But unfortunatelly the code for retrieving this parameter may be not implemented in motherboard drivers or MS drivers. In that case the result of VARIANT type will be set to NULL.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Aaron Altman |
| Solution 3 | Carsten |
| Solution 4 |
