c++获取Windows cpu温度,好像wmi或者Windows api能实现吗(如果用的是AI,能不能先在自己电脑上运行一下代码,AI的代码大概率是运行不了的)
7条回答 默认 最新
关注
WMI的话记得有一个,但是Windows api估计要找些开源库才有
WMI(Windows Management Instrumentation)获取Windows操作系统的CPU温度
WMI提供了一种用于获取系统信息的机制,包括CPU温度。你可以使用WMI查询语言(WQL)来获取相关信息。#include <iostream> #include <comdef.h> #include <Wbemidl.h> using namespace std; int main() { HRESULT hres; // 初始化COM库 hres = CoInitializeEx(0, COINIT_MULTITHREADED); if (FAILED(hres)) { cout << "Failed to initialize COM library. Error code: " << hex << hres << endl; return 1; } // 设置安全性级别 hres = CoInitializeSecurity( NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL ); if (FAILED(hres)) { cout << "Failed to initialize security. Error code: " << hex << hres << endl; CoUninitialize(); return 1; } IWbemLocator* pLoc = NULL; hres = CoCreateInstance( CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*)&pLoc ); if (FAILED(hres)) { cout << "Failed to create IWbemLocator object. Error code: " << hex << hres << endl; CoUninitialize(); return 1; } // 连接WMI IWbemServices* pSvc = NULL; hres = pLoc->ConnectServer( _bstr_t(L"ROOT\\CIMV2"), NULL, NULL, 0, NULL, 0, 0, &pSvc ); if (FAILED(hres)) { cout << "Failed to connect to WMI. Error code: " << hex << hres << endl; pLoc->Release(); CoUninitialize(); return 1; } // 设置代理 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)) { cout << "Failed to set proxy blanket. Error code: " << hex << hres << endl; pSvc->Release(); pLoc->Release(); CoUninitialize(); return 1; } // 查询数据 IEnumWbemClassObject* pEnumerator = NULL; hres = pSvc->ExecQuery( bstr_t("WQL"), bstr_t("SELECT * FROM Win32_PerfFormattedData_Counters_ThermalZoneInformation"), WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator ); if (FAILED(hres)) { cout << "Failed to execute WQL query. Error code: " << hex << hres << endl; pSvc->Release(); pLoc->Release(); CoUninitialize(); return 1; } // 遍历结果 IWbemClassObject* pclsObj = NULL; ULONG uReturn = 0; while (pEnumerator) { HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); if (0 == uReturn) { break; } VARIANT vtProp; hr = pclsObj->Get(L"Temperature", 0, &vtProp, 0, 0); if (SUCCEEDED(hr)) { wcout << "CPU Temperature: " << vtProp.uintVal << " degrees Celsius" << endl; VariantClear(&vtProp); } pclsObj->Release(); } // 清理资源 pSvc->Release(); pLoc->Release(); pEnumerator->Release(); CoUninitialize(); return 0; }
这段代码创建了一个与WMI相关的COM对象,连接到WMI,并使用WQL查询
Win32_PerfFormattedData_Counters_ThermalZoneInformation
类获取CPU温度信息。不同的计算机和操作系统版本可能有所不同,因此需要根据实际情况进行调整。解决 无用评论 打赏 举报
悬赏问题
- ¥15 安霸cv22 + rtl8211f 千兆,udp传输丢包
- ¥15 关于区块链和边缘环境搭建的相关问题
- ¥15 windows远程桌面断卡重连软件卡顿问题
- ¥30 Unity 实现扫描效果
- ¥15 HbuilderX检测不到安卓模拟器
- ¥15 这个main已经在filename.obj中定义是什么错 C语言
- ¥15 关于#linux#的问题:exsi8.0系统 怎么更改web访问端口,不用80、443
- ¥15 使用elementor设计样式
- ¥15 谁能提供一个中文版的推销咨询网站连接?
- ¥15 springboot项目程序启动报错