2024年2月7日发(作者:)
C++之WMI编程
大家平时在用类似鲁大师之类软件的时候,会看到鲁大师之类的软件能够获取到PC机的硬件的详细信息,有时候自己在写软件的时候也需要获取PC硬件的详细信息,这个时候你会发现有的硬件信息通过系统的API函数无法获取到或者获取的不准确。所以这个时候就要通过WMI编程来获取硬件信息了。WMI是windows操作系统用来管理软件和硬件的核心。其它的不再多讲了,下面来看最主要的,通过WMI获取硬件信息。
整体来说,通过WMI获取计算机硬件信息有以下几个步骤
1、 用CoInitializeEx函数初始化COM参数。因为WMI中的类都是基于COM技术的。
2、 用CoInitializeSecurity函数初始化COM进程的安全,因为WMI的进程和应用程序进程不在同一个级别。
3、 获取IWbemServices指针,通过函数IWbemLocator::ConnectServer获取。
4、 通过CoSetProxyBlanket设置IWbemServices代理安全,使WMI服务可以模拟客户端。
5、 使用IWbemServices进行WMI查询,主要使用WQL语句。
6、 清理COM对象。
例子, 查询操作系统信息的一个例子
#include "stdafx.h"
#include
using namespace std;
#include
#include
# pragma comment(lib, "")
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hres;
// Initialize COM.
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. "
<< "Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Initialize
hres = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Obtain the initial locator to Windows Management
// on a particular host computer.
IWbemLocator *pLoc = 0;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object. "
<< "Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
IWbemServices *pSvc = 0;
// Connect to the rootcimv2 namespace with the
// current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOTCIMV2"), // WMI namespace
NULL, // User name
NULL, // User password
0, // Locale
NULL, // Security flags
0, // Authority
0, // Context object
&pSvc // IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOTCIMV2 WMI namespace" << endl;
// Set the IWbemServices proxy so that impersonation
// of the user (client) occurs.
hres = CoSetProxyBlanket(
pSvc, // the proxy to set
RPC_C_AUTHN_WINNT, // authentication service
RPC_C_AUTHZ_NONE, // authorization service
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_OperatingSystem"),
WBEM_FLAG_FORWARD_ONLY |
WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for processes failed. "
<< "Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
else
}
{
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0;
while (pEnumerator)
{
hres = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
}
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
// Get the value of the Name property
hres = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << "Manufacturer Name : " << l << endl;
VariantClear(&vtProp);
其它的查询可以修改WQL语句来实现。


发布评论