• 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}");
    }
  • 相关阅读:
    no,no,不要使用kill -9
    Linux中etc目录详解
    Quartz任务调度器的使用
    RMI(Remote Method invocation,远程方法访问)
    SilverLight扩展控件RadTreeView
    SiverLight和HTML交互
    SilverLight控件之ContextMenu和RadContextMenu(菜单)
    SilverLight之向后台请求数据-WebClient
    SilverLight控件样式及控件模版
    在SilverLight中代码编写可选择树节点
  • 原文地址:https://www.cnblogs.com/jerry1999/p/3677345.html
Copyright © 2020-2023  润新知