您当前的位置: 首页 >  ui

蓝不蓝编程

暂无认证

  • 2浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

蓝不蓝编程 发布时间:2013-03-12 21:56:32 ,浏览量:2

#pragma hdrstop #include #include #include 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}"); }
关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0437s