C++ 不用wmi 获取所有硬件名称和序列号包括主板,内存,硬盘,显示器
时间: 2023-06-26 19:09:17 浏览: 808
在 C++ 中,可以使用 Windows Management Instrumentation (WMI) 来获取计算机硬件的信息。以下是一个示例代码,可以获取主板、内存、硬盘和显示器的名称和序列号:
```
#include <iostream>
#include <windows.h>
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
int main()
{
HRESULT hres;
// Initialize COM
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
std::cout << "Failed to initialize COM library. Error code = " << hres << std::endl;
return 1;
}
// Initialize security
hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hres))
{
std::cout << "Failed to initialize security. Error code = " << hres << std::endl;
CoUninitialize();
return 1;
}
// Obtain the initial locator to WMI
IWbemLocator* pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID*)&pLoc
);
if (FAILED(hres))
{
std::cout << "Failed to create IWbemLocator object. Error code = " << hres << std::endl;
CoUninitialize();
return 1;
}
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices* pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hres))
{
std::cout << "Could not connect. Error code = " << hres << std::endl;
pLoc->Release();
CoUninitialize();
return 1;
}
// Set security levels on the proxy
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))
{
std::cout << "Could not set proxy blanket. Error code = " << hres << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Use the IWbemServices pointer to make requests of WMI
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
_bstr_t("WQL"),
_bstr_t("SELECT * FROM Win32_BaseBoard"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hres))
{
std::cout << "Query for Win32_BaseBoard failed. Error code = " << hres << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
// Get the data from the query
IWbemClassObject* pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0)
{
break;
}
VARIANT vtProp;
hres = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
std::cout << "BaseBoard Serial Number : " << ((vtProp.vt == VT_NULL) ? "NULL" : _com_util::ConvertBSTRToString(vtProp.bstrVal)) << std::endl;
hres = pclsObj->Get(L"Product", 0, &vtProp, 0, 0);
std::cout << "BaseBoard Product : " << ((vtProp.vt == VT_NULL) ? "NULL" : _com_util::ConvertBSTRToString(vtProp.bstrVal)) << std::endl;
VariantClear(&vtProp);
pclsObj->Release();
}
pEnumerator->Release();
hres = pSvc->ExecQuery(
_bstr_t("WQL"),
_bstr_t("SELECT * FROM Win32_PhysicalMemory"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hres))
{
std::cout << "Query for Win32_PhysicalMemory failed. Error code = " << hres << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0)
{
break;
}
VARIANT vtProp;
hres = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
std::cout << "Memory Serial Number : " << ((vtProp.vt == VT_NULL) ? "NULL" : _com_util::ConvertBSTRToString(vtProp.bstrVal)) << std::endl;
hres = pclsObj->Get(L"PartNumber", 0, &vtProp, 0, 0);
std::cout << "Memory Part Number : " << ((vtProp.vt == VT_NULL) ? "NULL" : _com_util::ConvertBSTRToString(vtProp.bstrVal)) << std::endl;
VariantClear(&vtProp);
pclsObj->Release();
}
pEnumerator->Release();
hres = pSvc->ExecQuery(
_bstr_t("WQL"),
_bstr_t("SELECT * FROM Win32_DiskDrive"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hres))
{
std::cout << "Query for Win32_DiskDrive failed. Error code = " << hres << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0)
{
break;
}
VARIANT vtProp;
hres = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
std::cout << "Disk Drive Serial Number : " << ((vtProp.vt == VT_NULL) ? "NULL" : _com_util::ConvertBSTRToString(vtProp.bstrVal)) << std::endl;
hres = pclsObj->Get(L"Model", 0, &vtProp, 0, 0);
std::cout << "Disk Drive Model : " << ((vtProp.vt == VT_NULL) ? "NULL" : _com_util::ConvertBSTRToString(vtProp.bstrVal)) << std::endl;
VariantClear(&vtProp);
pclsObj->Release();
}
pEnumerator->Release();
hres = pSvc->ExecQuery(
_bstr_t("WQL"),
_bstr_t("SELECT * FROM Win32_DesktopMonitor"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hres))
{
std::cout << "Query for Win32_DesktopMonitor failed. Error code = " << hres << std::endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (uReturn == 0)
{
break;
}
VARIANT vtProp;
hres = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
std::cout << "Monitor Serial Number : " << ((vtProp.vt == VT_NULL) ? "NULL" : _com_util::ConvertBSTRToString(vtProp.bstrVal)) << std::endl;
hres = pclsObj->Get(L"MonitorType", 0, &vtProp, 0, 0);
std::cout << "Monitor Type : " << ((vtProp.vt == VT_NULL) ? "NULL" : _com_util::ConvertBSTRToString(vtProp.bstrVal)) << std::endl;
VariantClear(&vtProp);
pclsObj->Release();
}
pEnumerator->Release();
// Cleanup
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0;
}
```
其中 `Win32_BaseBoard` 是获取主板信息的 WMI 类,`Win32_PhysicalMemory` 是获取内存信息的 WMI 类,`Win32_DiskDrive` 是获取硬盘信息的 WMI 类,`Win32_DesktopMonitor` 是获取显示器信息的 WMI 类。以上代码可以在 Windows 平台上编译运行,并输出对应硬件的名称和序列号。
阅读全文