C++ builder 通过WMI查询网卡对应的序号

本文介绍了一段C++代码,该代码通过WMI(Windows Management Instrumentation)查询特定网络适配器配置的索引。使用了COM组件对象模型进行初始化,并设置安全性级别,然后创建IWbemLocator实例连接到WMI命名空间,执行WQL查询以找到匹配的适配器。
摘要由CSDN通过智能技术生成
#pragma hdrstop


#include <wbemcli.h>
#include <comdef.h>
#include <vcl.h>


using namespace std;
#pragma argsused
int getAdapterIndex(String adapterName) {
int adapterIndex = -1;
BSTR resPath = L"root\\cimv2";


// Initialize COM
HRESULT hres;
// hres = CoInitializeEx(0, COINIT_MULTITHREADED);
// if (FAILED(hres)) {
// return 1;
// }


// Set general COM security levels
hres = CoInitializeSecurity(NULL, -1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);


if (FAILED(hres)) {
CoUninitialize();
return 1; // Program has failed.
}


// Obtain the initial locator to WMI
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);


if (FAILED(hres)) {
CoUninitialize();
return 1;
}


// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;


hres = pLoc->ConnectServer(resPath, // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);


if (FAILED(hres)) {
pLoc->Release();
CoUninitialize();
return 1;
}


// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);


if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}


// Use the IWbemServices pointer to make requests of WMI
IEnumWbemClassObject* pEnumerator = NULL;
String queryStr = L"Select * from Win32_NetworkAdapterConfiguration where settingid='"+adapterName+"'";
hres =
pSvc->ExecQuery(
L"WQL",
queryStr.w_str(),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL,
&pEnumerator);


if (FAILED(hres)) {
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}


// Get the data from the WQL sentence
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;


while (pEnumerator) {
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn);
if (0 == uReturn || FAILED(hr))
break;
VARIANT vtProp;
hr = pclsObj->Get(L"Index", 0, &vtProp, 0, 0);
if (!FAILED(hr)) {
adapterIndex = vtProp.iVal;  //Get Adapter Index
}
VariantClear(&vtProp);


pclsObj->Release();
pclsObj = NULL;
}


// Cleanup
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
if (pclsObj != NULL)
pclsObj->Release();


CoUninitialize();
return adapterIndex;
}
int main(int argc, char* argv[]) {
int adapterIndex = getAdapterIndex("{4341FA83-5918-46D8-BF43-6C96A0954D3E}");
}

转载于:https://www.cnblogs.com/jerry1999/archive/2013/03/12/3677345.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值