|
VC++使用WMI来获取CPU的名称、主频和核心数
要更准确地获取CPU的主频,我们可以使用Windows Management Instrumentation (WMI)。以下是改进后的代码示例,使用WMI来获取CPU的名称、主频和核心数:
- #include <windows.h>
- #include <iostream>
- #include <string>
- #include <comdef.h>
- #include <wbemidl.h>
- #pragma comment(lib, "wbemuuid.lib")
- std::string GetCPUName() {
- std::string cpuName;
- HRESULT hres;
- // 初始化COM库
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres)) {
- return "无法初始化COM库";
- }
- // 初始化COM
- hres = CoInitializeSecurity(NULL,
- -1, // COM negotiates service
- NULL, // Authentication services
- NULL, // Reserved
- RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
- RPC_C_IMP_LEVEL_IMPERSONATE,// Impersonation
- NULL, // Reserved
- EOAC_NONE, // Additional capabilities
- NULL // Reserved
- );
- // 创建WMI接口
- IWbemLocator *pLoc = NULL;
- hres = CoCreateInstance(
- CLSID_WbemLocator,
- 0,
- CLSCTX_INPROC_SERVER,
- IID_IWbemLocator, (LPVOID *)&pLoc);
- IWbemServices *pSvc = NULL;
- // 连接到WMI命名空间
- hres = pLoc->ConnectServer(
- _bstr_t(L"ROOT\\CIMV2"), // WMI命名空间
- NULL, // 用户名
- NULL, // 密码
- 0, // Locale
- NULL, // Security flags
- 0, // Authority
- 0, // Context object
- &pSvc // IWbemServices代理
- );
- // 设置安全级别
- hres = CoInitializeSecurity(
- NULL,
- -1, // COM negotiates service
- NULL, // Authentication services
- NULL, // Reserved
- RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
- RPC_C_IMP_LEVEL_IMPERSONATE,// Impersonation
- NULL, // Reserved
- EOAC_NONE, // Additional capabilities
- NULL // Reserved
- );
- // 查询CPU信息
- IEnumWbemClassObject* pEnumerator = NULL;
- hres = pSvc->ExecQuery(
- bstr_t("WQL"),
- bstr_t("SELECT Name, MaxClockSpeed, NumberOfCores FROM Win32_Processor"),
- WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
- NULL,
- &pEnumerator);
- IWbemClassObject *pclsObj = NULL;
- ULONG uReturn = 0;
- while (pEnumerator) {
- HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
- if (0 == uReturn) {
- break;
- }
- VARIANT vtProp;
- // 获取CPU名称
- hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
- cpuName = _bstr_t(vtProp.bstrVal);
- VariantClear(&vtProp);
- }
- // 清理
- pSvc->Release();
- pLoc->Release();
- pEnumerator->Release();
- pclsObj->Release();
- CoUninitialize();
- return cpuName;
- }
- int GetCPUCores() {
- int cpuCores = 0;
- HRESULT hres;
- // 初始化COM库
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres)) {
- return -1;
- }
- // 创建WMI接口
- IWbemLocator *pLoc = NULL;
- hres = CoCreateInstance(
- CLSID_WbemLocator,
- 0,
- CLSCTX_INPROC_SERVER,
- IID_IWbemLocator, (LPVOID *)&pLoc);
- IWbemServices *pSvc = NULL;
- // 连接到WMI命名空间
- hres = pLoc->ConnectServer(
- _bstr_t(L"ROOT\\CIMV2"),
- NULL,
- NULL,
- 0,
- NULL,
- 0,
- 0,
- &pSvc
- );
- // 查询CPU信息
- IEnumWbemClassObject* pEnumerator = NULL;
- hres = pSvc->ExecQuery(
- bstr_t("WQL"),
- bstr_t("SELECT NumberOfCores FROM Win32_Processor"),
- WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
- NULL,
- &pEnumerator);
- IWbemClassObject *pclsObj = NULL;
- ULONG uReturn = 0;
- while (pEnumerator) {
- HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
- if (0 == uReturn) {
- break;
- }
- VARIANT vtProp;
- // 获取CPU核心数
- hr = pclsObj->Get(L"NumberOfCores", 0, &vtProp, 0, 0);
- cpuCores = vtProp.uintVal;
- VariantClear(&vtProp);
- }
- // 清理
- pSvc->Release();
- pLoc->Release();
- pEnumerator->Release();
- pclsObj->Release();
- CoUninitialize();
- return cpuCores;
- }
- int GetCPUFrequency() {
- int cpuFrequency = 0;
- HRESULT hres;
- // 初始化COM库
- hres = CoInitializeEx(0, COINIT_MULTITHREADED);
- if (FAILED(hres)) {
- return -1;
- }
- // 创建WMI接口
- IWbemLocator *pLoc = NULL;
- hres = CoCreateInstance(
- CLSID_WbemLocator,
- 0,
- CLSCTX_INPROC_SERVER,
- IID_IWbemLocator, (LPVOID *)&pLoc);
- IWbemServices *pSvc = NULL;
- // 连接到WMI命名空间
- hres = pLoc->ConnectServer(
- _bstr_t(L"ROOT\\CIMV2"),
- NULL,
- NULL,
- 0,
- NULL,
- 0,
- 0,
- &pSvc
- );
- // 查询CPU信息
- IEnumWbemClassObject* pEnumerator = NULL;
- hres = pSvc->ExecQuery(
- bstr_t("WQL"),
- bstr_t("SELECT MaxClockSpeed FROM Win32_Processor"),
- WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
- NULL,
- &pEnumerator);
- IWbemClassObject *pclsObj = NULL;
- ULONG uReturn = 0;
- while (pEnumerator) {
- HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
- if (0 == uReturn) {
- break;
- }
- VARIANT vtProp;
- // 获取CPU主频
- hr = pclsObj->Get(L"MaxClockSpeed", 0, &vtProp, 0, 0);
- cpuFrequency = vtProp.uintVal;
- VariantClear(&vtProp);
- }
- // 清理
- pSvc->Release();
- pLoc->Release();
- pEnumerator->Release();
- pclsObj->Release();
- CoUninitialize();
- return cpuFrequency;
- }
- int main() {
- std::string cpuName = GetCPUName();
- int cpuCores = GetCPUCores();
- int cpuFrequency = GetCPUFrequency();
- std::cout << "CPU名称: " << cpuName << std::endl;
- std::cout << "CPU核心数: " << cpuCores << std::endl;
- std::cout << "CPU主频: " << cpuFrequency << " MHz" << std::endl;
- return 0;
- }
复制代码
### 说明:
1. **COM初始化**:使用 `CoInitializeEx` 和 `CoInitializeSecurity` 初始化COM库。
2. **WMI查询**:通过WMI查询获取CPU的信息,包括名称、主频和核心数。
3. **清理资源**:在最后释放WMI接口和COM库。
### 注意事项:
- 确保在项目中链接 `wbemuuid.lib` 库。
- 需要在Windows环境下运行此代码。
- 可能需要以管理员权限运行程序,以便访问WMI信息。
|
|