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 如何让企业微信机器人实现消息汇总整合
- ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
- ¥15 如何用Python爬取各高校教师公开的教育和工作经历
- ¥15 TLE9879QXA40 电机驱动
- ¥20 对于工程问题的非线性数学模型进行线性化
- ¥15 Mirare PLUS 进行密钥认证?(详解)
- ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
- ¥20 想用ollama做一个自己的AI数据库
- ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
- ¥15 请问怎么才能复现这样的图呀