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


    #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}");
    }
  • 相关阅读:
    Java数据结构学习之Vector和Stack
    System.arraycopy方法的使用
    git提交的时候,报错yarn run v1.21.1 ,SyntaxError: Cannot use import statement outside a module 解决
    关于使用antdproTable,报错 ResizeObserver loop limit exceeded
    【数据库数据恢复】Sql Server数据库数据恢复案例
    【存储数据恢复】服务器存储上的raid5阵列崩溃的数据恢复案例
    【raid数据恢复】光纤存储raid阵列数据恢复案例
    【服务器raid数据恢复】光纤存储raid数据恢复案例
    【虚拟机数据恢复】Linux系统下误删除KVM虚拟机的数据恢复案例
    【服务器数据恢复】服务器raid5磁盘阵列分区丢失的数据恢复案例
  • 原文地址:https://www.cnblogs.com/jerry1999/p/3677345.html
Copyright © 2020-2023  润新知