Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; } 
like image 262
Johnny Mnemonic Avatar asked Apr 26 '14 18:04

Johnny Mnemonic


People also ask

Can you check CPU temp Windows 10?

There is no such option to check CPU temperature in Windows 10. You can either check the temperature in BIOS or you can use third-party applications.

Can you check CPU temp without software?

You can estimate the CPU temperature using your hand. If you place your hand on your computer and you feel heat, that means it's at least 120 degrees Fahrenheit. If it's too hot to touch, it's probably around 140 degrees Fahrenheit. My PC fans are going faster, but the temp is fine on and in the PC.

How do I check my CPU temp in CMD?

How can I get the CPU temperature in a cmd shell? Try the following. function Get-Temperature { $t = Get-WmiObject MSAcpi_ThermalZoneTemperature -Namespace "root/wmi" $currentTempKelvin = $t.


1 Answers

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 this 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 is confirmed to get the temperature of an Intel Haswell CPU in Celcius successfully. 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.

like image 103
tomer zeitune Avatar answered Oct 08 '22 20:10

tomer zeitune