• C++ builder 通过WMI方式修改DNS


    #pragma hdrstop
    #include <iostream>
    using namespace std;
    #include <wbemcli.h>
    #pragma comment(lib, "wbemUuid2.lib")
    #pragma argsused
    void PrintWMIError(HRESULT hr);
    HRESULT modifyDNS() {
    HRESULT hr;
    hr = CoInitializeEx(0, COINIT_MULTITHREADED);
    hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
    RPC_C_AUTHN_LEVEL_PKT_PRIVACY, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,
    EOAC_SECURE_REFS, NULL);


    IWbemLocator *pLocator = NULL;
    IWbemServices *pNamespace = NULL;
    IWbemClassObject *pClass = NULL;
    IWbemClassObject *pInputParamClass = NULL;
    IWbemClassObject *pInputParamInstance = NULL;
    IWbemClassObject *pOutInst = NULL;
    BSTR InstancePath = SysAllocString(
    L"Win32_NetworkAdapterConfiguration.index='7'"); //index为网卡号
    BSTR Path = SysAllocString(L"root\\cimv2");
    BSTR ClassPath = SysAllocString(L"Win32_NetworkAdapterConfiguration");
    BSTR MethodName1 = SysAllocString(L"SetDNSServerSearchOrder");
    BSTR MethodName2 = SysAllocString(L"EnableDHCP");
    LPCWSTR MethodName1ArgName = L"DNSServerSearchOrder";
    BSTR dns1 = SysAllocString(L"10.0.0.22");
    BSTR dns2 = SysAllocString(L"10.0.0.23");


    long DnsIndex1[] = { 0 };
    long DnsIndex2[] = { 1 };


    SAFEARRAY *ip_list = SafeArrayCreateVector(VT_BSTR, 0, 2);
    SafeArrayPutElement(ip_list, DnsIndex1, dns1);
    SafeArrayPutElement(ip_list, DnsIndex2, dns2);
    VARIANT dns;
    dns.vt = VT_ARRAY | VT_BSTR;
    dns.parray = ip_list;


    hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
    IID_IWbemLocator, (void **) &pLocator);
    hr = pLocator->ConnectServer(Path, NULL, NULL, NULL, 0, NULL, NULL,
    &pNamespace);
    hr = CoSetProxyBlanket(pNamespace, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
    NULL, RPC_C_AUTHN_LEVEL_CALL, RPC_C_IMP_LEVEL_IMPERSONATE, NULL,
    EOAC_NONE);
    if (!SUCCEEDED(hr)) {
    PrintWMIError(hr);
    return hr;
    }
    if (SUCCEEDED(hr))
    hr = pNamespace->GetObject(ClassPath, 0, NULL, &pClass, NULL);
    if (!SUCCEEDED(hr)) {
    PrintWMIError(hr);
    return hr;
    }
    if (SUCCEEDED(hr))
    hr = pClass->GetMethod(MethodName1, 0, &pInputParamClass, NULL);
    if (!SUCCEEDED(hr)) {
    PrintWMIError(hr);
    return hr;
    }
    if (SUCCEEDED(hr))
    hr = pInputParamClass->SpawnInstance(0, &pInputParamInstance);
    if (!SUCCEEDED(hr)) {
    PrintWMIError(hr);
    return hr;
    }
    if (SUCCEEDED(hr)) {
    hr = pInputParamInstance->Put(MethodName1ArgName, 0, &dns, 0);
    }


    if (!SUCCEEDED(hr)) {
    PrintWMIError(hr);
    return hr;
    }
    if (SUCCEEDED(hr))
    hr = pNamespace->ExecMethod(InstancePath, MethodName1, 0, NULL,
    pInputParamInstance, &pOutInst, NULL);
    if (!SUCCEEDED(hr)) {
    PrintWMIError(hr);
    return hr;
    }
    if (SUCCEEDED(hr))
    hr = pNamespace->ExecMethod(InstancePath, MethodName2, 0, NULL, NULL,
    &pOutInst, NULL);
    if (!SUCCEEDED(hr)) {
    PrintWMIError(hr);
    return hr;
    }
    SysFreeString(InstancePath);
    SysFreeString(Path);
    SysFreeString(ClassPath);
    SysFreeString(MethodName1);
    SysFreeString(MethodName2);
    if (pOutInst)
    pOutInst->Release();
    if (pInputParamInstance)
    pInputParamInstance->Release();
    if (pInputParamClass)
    pInputParamClass->Release();
    if (pClass)
    pClass->Release();
    if (pNamespace)
    pNamespace->Release();
    if (pLocator)
    pLocator->Release();
    CoUninitialize();
    return hr;
    }


    int main(int argc, char* argv[]) {
    modifyDNS();
    }


    void PrintWMIError(HRESULT hr) {
    IWbemStatusCodeText * pStatus = NULL;
    HRESULT hres = CoCreateInstance(CLSID_WbemStatusCodeText, 0,
    CLSCTX_INPROC_SERVER, IID_IWbemStatusCodeText, (LPVOID *) &pStatus);
    if (S_OK == hres) {
    BSTR bstrError;
    hres = pStatus->GetErrorCodeText(hr, 0, 0, &bstrError);
    if (S_OK != hres)
    bstrError = SysAllocString(L"Get     last     error     failed");
    BSTR bstrTError = bstrError;
    wchar_t* lpszError = bstrTError;
    printf("%s\n", lpszError);
    pStatus->Release();
    SysFreeString(bstrError);
    }
    }
  • 相关阅读:
    [结题报告]10235 Simply Emirp Time limit: 3.000 seconds
    [杭电ACM]1720A+B Coming
    [结题报告]10041 Vito's Family Time limit: 3.000 seconds
    mysqlvarchar、text 类型到底能存储多大字符?
    mysql项目更换数据源为oralce后项目调整
    Oracle创建定时任务执行函数
    IOS手机访问网页window.location.href跳转新页面第一次可以第二次报错失效
    logrotate日志分割
    查找一批设备的在线情况
    pkill 用法例子
  • 原文地址:https://www.cnblogs.com/jerry1999/p/3677349.html
Copyright © 2020-2023  润新知