c++ 代码获取CPU温度
时间: 2023-02-15 17:49:44 浏览: 21
```
import psutil
def get_cpu_temperature():
temperature = psutil.sensors_temperatures()['cpu-thermal'][0].current
return temperature
print(get_cpu_temperature())
```
相关问题
请问c++如何获取cpu温度
可以使用一些系统监控工具来获取 CPU 温度,比如 Linux 下可以使用 lm-sensors 和 psensor 等工具,Windows 下可以使用 HWiNFO、Core Temp 等工具。还可以通过代码方式获取,比如在 Linux 下可以通过读取 /sys/class/thermal/thermal_zoneX/temp 文件来获取 CPU 温度。
c++ 获取cpu温度
在C语言中获取CPU温度需要调用操作系统提供的API,因为不同操作系统提供的API不同,所以获取CPU温度的方法也会有所不同。
在Windows系统中,可以使用WMI(Windows Management Instrumentation)来获取CPU温度。具体的代码如下:
```
#include <windows.h>
#include <wbemidl.h>
#include <stdio.h>
#pragma comment(lib, "wbemuuid.lib")
int main()
{
HRESULT hres;
// 初始化COM库
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
printf("Failed to initialize COM library. Error code = 0x%x\n", hres);
return 1;
}
// 设置COM库的安全级别
hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hres))
{
printf("Failed to initialize security. Error code = 0x%x\n", hres);
CoUninitialize();
return 1;
}
// 创建WMI服务
IWbemLocator* pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID*)&pLoc
);
if (FAILED(hres))
{
printf("Failed to create IWbemLocator object. Error code = 0x%x\n", hres);
CoUninitialize();
return 1;
}
// 连接WMI服务
IWbemServices* pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\WMI"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hres))
{
printf("Could not connect. Error code = 0x%x\n", hres);
pLoc->Release();
CoUninitialize();
return 1;
}
// 设置WMI服务的安全级别
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres))
{
printf("Could not set proxy blanket. Error code = 0x%x\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// 构造WMI查询语句
BSTR bstrQuery = SysAllocString(L"SELECT * FROM MSAcpi_ThermalZoneTemperature");
BSTR bstrWQL = SysAllocString(L"WQL");
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstrWQL,
bstrQuery,
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hres))
{
printf("Query failed. Error code = 0x%x\n", hres);
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// 获取查询结果
IWbemClassObject* pclsObj = NULL;
ULONG uReturn = 0;
VARIANT vtProp;
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (0 == uReturn)
{
break;
}
// 获取CPU温度
hres = pclsObj->Get(L"CurrentTemperature", 0, &vtProp, 0, 0);
if (FAILED(hres))
{
printf("Could not get CPU temperature. Error code = 0x%x\n", hres);
pclsObj->Release();
continue;
}
// 将温度值转换为摄氏度
double temperature = vtProp.intVal / 10.0 - 273.15;
// 输出温度值
printf("CPU temperature: %.2f℃\n", temperature);
VariantClear(&vtProp);
pclsObj->Release();
}
// 释放资源
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return 0;
}
```
在Linux系统中,可以通过读取/sys/class/thermal/thermal_zone0/temp文件来获取CPU温度。具体的代码如下:
```
#include <stdio.h>
int main()
{
FILE* fp = fopen("/sys/class/thermal/thermal_zone0/temp", "r");
if (fp == NULL)
{
printf("Failed to open file.\n");
return 1;
}
int temperature;
fscanf(fp, "%d", &temperature); // 读取温度值
fclose(fp);
// 将温度值转换为摄氏度
double celsius = temperature / 1000.0;
// 输出温度值
printf("CPU temperature: %.2f℃\n", celsius);
return 0;
}
```
阅读全文