C++ builder 通过WMI查询网卡对应的序号
2013-03-12 21:56 每天努力一点点 阅读(311) 评论(0) 编辑 收藏 举报
#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}");
}
#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}");
}