使用WMI更改IP地址

Vista下面没有办法使用DhcpNotifyConfigChange来通知适配器配置的改变,尝试用wmi来实现修改ip地址,c++源码如下(.Net2003下面编译运行通过)

#define _WIN32_DCOM

#i nclude <comdef.h>

#i nclude <Wbemidl.h>

#i nclude "atlbase.h"

# pragma comment(lib, "wbemuuid.lib")

IWbemLocator* g_pLoc = NULL;

IWbemServices* g_pSvc = NULL;

IEnumWbemClassObject* g_pEnum = NULL;

IWbemClassObject* g_pAdapterObject = NULL;

IWbemClassObject* g_pAdapterConfigClass = NULL;

VARIANT g_AdapterConfigPathVal;

 

USHORT ANWMIInit(PCWSTR pszAdapterName)

{

    HRESULT hres;

    CComBSTR TheQuery = NULL;

    ULONG NumObjects = 0;

 

    // Step 1: Initialize COM.

    hres CoInitializeEx(0, COINIT_MULTITHREADED);

    if (FAILED(hres))

    {

       OutputDebugString("CoInitializeEx failed!!/n");

       return AE_SET;                 

    }

 

    // Step 2: Set general COM security levels

    // Note: If you are using Windows 2000, you need to specify -

    // the default authentication credentials for a user by using

    // a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----

    // parameter of CoInitializeSecurity ------------------------

    hres CoInitializeSecurity(

       NULL,

       -1,                          // COM negotiates service

       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))

    {

       OutputDebugString("CoInitializeSecurity failed!!/n");

       return AE_SET;                    

    }

 

    // Step 3:  Obtain the initial locator to WMI

    hres = CoCreateInstance(

       CLSID_WbemLocator,            

       0,

       CLSCTX_INPROC_SERVER,

       IID_IWbemLocator,

       (LPVOID*)&g_pLoc);

    if (FAILED(hres))

    {

       OutputDebugString("CoCreateInstance failed!!/n");

       return AE_SET;               

    }

 

    // Step 4: Connect to the local root/cimv2 namespace and obtain pointer pSvc to make IWbemServices calls.

    hres = g_pLoc->ConnectServer(

       _bstr_t(L"ROOT//CIMV2"),

       NULL,

       NULL,

       0,

       NULL,

       0,

       0,

       &g_pSvc

       );

    if (FAILED(hres))

    {

       OutputDebugString("ConnectServer failed!!/n");

       return AE_SET;              

    }

 

    // Step 5:  Set security levels for the proxy

    hres = CoSetProxyBlanket(

       g_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))

    {

       OutputDebugString("CoSetProxyBlanket failed!!/n");

       return AE_SET;             

    } 

 

// 通过适配器名称来找到指定的适配器对象.

    TheQuery = L"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE SettingID = /"";

    TheQuery += pszAdapterName;

    TheQuery += L"/"";  

    hres = g_pSvc->ExecQuery(

       //SysAllocString(L"WQL"),

       L"WQL",

       TheQuery,

       WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,

       NULL,

       &g_pEnum);

    if (FAILED(hres))

    {

       OutputDebugString("ExecQuery failed!!/n");

       return AE_SET;             

    }

 

    // Get the adapter object.

    hres = g_pEnum->Next(WBEM_INFINITE, 1, &g_pAdapterObject, &NumObjects);

    if (SUCCEEDED(hres))

    {

       if (NumObjects < 1)

       {

           OutputDebugString("Can not get the specified adapter!!/n");

           return AE_SET;

       }

    }

hres = g_pSvc->GetObject(

       L"Win32_NetworkAdapterConfiguration",

       0,

       NULL,

       &g_pAdapterConfigClass,

       NULL);

    if (FAILED(hres))

    {

       OutputDebugString("GetObject failed!!/n");

       return AE_SET;              

    }

 

    hres = g_pAdapterObject->Get(

       L"__RELPATH",

       0,

       &g_AdapterConfigPathVal,

       NULL,

       NULL);

    if (FAILED(hres))

    {

       OutputDebugString("Get failed!!/n");

       return AE_SET;            

    }

 

    return AE_SUCCESS;

}

 

void ANWMIRelease()

{

    VariantClear(&g_AdapterConfigPathVal);

    if (g_pAdapterConfigClass)

    {

       g_pAdapterConfigClass->Release();

       g_pAdapterConfigClass = NULL;

    }

    if (g_pAdapterObject)

    {

       g_pAdapterObject->Release();

       g_pAdapterObject = NULL;

    }

    if (g_pEnum)

    {

       g_pEnum->Release();

       g_pEnum = NULL;

    }

    if (g_pSvc)

    {

       g_pSvc->Release();

        g_pSvc = NULL;

    }

    if (g_pLoc)

    {

       g_pLoc->Release();

       g_pLoc = NULL;

    }   

    CoUninitialize();

}


USHORT ANEnableStaticInVista(PIP_ADAPTER_INDEX_MAP pimAdapter, PMACDATA pData)

{

    HRESULT hres;

    USHORT usStatus = AE_SET;

 

    //VARIANT AdapterConfigPathVal;

    //IWbemClassObject* pAdapterConfigClass = NULL;

    IWbemClassObject* pInParamsDefinition = NULL;

    IWbemClassObject* pInParams = NULL;

 

    struct in_addr in;

    SAFEARRAY* pIpAddress = NULL;

    SAFEARRAY* pSubnetMask = NULL;

    SAFEARRAY* pGatewayAddress = NULL;

    SAFEARRAYBOUND bound[1];

    long i = 0;

    VARIANT ArrayVal;

 

    IWbemClassObject* pOutParams = NULL;

    VARIANT varReturnValue;

    //WCHAR address[260];

    CComBSTR IPAddress;

    CComBSTR MASKAddress;

    CComBSTR Gateway;

 

    if (AE_SUCCESS != ANWMIInit(wcschr(pimAdapter->Name, L'{')))

    {

       goto END;

    }

 

    if ((pData->ulStateMask & AS_IP_CHANGED) || (pData->ulStateMask & AS_MASK_CHANGED))

    {

       DWORD dwIP = htonl(pData->ulIP);

       DWORD dwMask = htonl(pData->ulMask);

       //Get the EnableStatic method and the in-params class.

       hres = g_pAdapterConfigClass->GetMethod(

           L"EnableStatic",

           0,

           &pInParamsDefinition,

           NULL);

       if (FAILED(hres))

       {

           OutputDebugString("GetMethod failed!!/n");

           goto END;

       }

 

       //Get the in-params object.

       hres = pInParamsDefinition->SpawnInstance(0, &pInParams);

       if (FAILED(hres))

       {

           OutputDebugString("SpawnInstance failed!!/n");

           goto END;

       }

 

       //Set the in-params values for ip address.

       bound[0].lLbound = 0;

       bound[0].cElements = 1;

       pIpAddress = SafeArrayCreate(VT_BSTR, 1, bound);

       if(pIpAddress == NULL)

       {

           OutputDebugString("SafeArrayCreate failed!!/n");

           goto END;

       }

       in.s_addr = dwIP;

       //AnsiToUnicode(inet_ntoa(in),address);

       IPAddress = inet_ntoa(in);

       OutputDebugString("the ip address is:");

       OutputDebugStringW(IPAddress);

       hres = SafeArrayPutElement(pIpAddress, &i, (BSTR)IPAddress);

       if (FAILED(hres))

       {

           OutputDebugString("SafeArrayPutElement failed!!/n");

           goto END;

       }

 

       //Set the in-params values for subnet mask address.

       pSubnetMask = SafeArrayCreate(VT_BSTR, 1, bound);

       if(pSubnetMask == NULL)

       {

           OutputDebugString("SafeArrayCreate failed!!/n");

           goto END;

       }

       in.s_addr = dwMask;

       //AnsiToUnicode(inet_ntoa(in),address);

       MASKAddress = inet_ntoa(in);

       OutputDebugString("the submask is:/n");

       OutputDebugStringW(MASKAddress);

       hres = SafeArrayPutElement(pSubnetMask, &i, (BSTR)MASKAddress);

       if (FAILED(hres))

       {

           OutputDebugString("SafeArrayPutElement failed!!/n");

           goto END;

       }

 

       // bind the in-params.

       ArrayVal.parray = pIpAddress;

       ArrayVal.vt = VT_ARRAY|VT_BSTR;

       hres = pInParams->Put(L"IPAddress", 0,&ArrayVal, 0);

       ArrayVal.parray = pSubnetMask;

       hres = pInParams->Put(L"SubnetMask", 0,   &ArrayVal, 0);

 

// Now actually execute the "EnableStatic" method

       hres = g_pSvc->ExecMethod(

           g_AdapterConfigPathVal.bstrVal,

           L"EnableStatic",

           0,

           NULL,

           pInParams,

           &pOutParams,

           NULL);

       if (FAILED(hres))

       {

           OutputDebugString("ExecMethod failed!!/n");

           goto END;

       }

 

       // Get the return value.

       VariantInit(&varReturnValue);

       hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0, &varReturnValue, NULL, 0);

       if (FAILED(hres))

       {

           OutputDebugString("Get return value failed!!/n");

           goto END;

       }

 

       // Check the return value.

       if (0 != varReturnValue.lVal)

       {

           char a[100];

           sprintf(a,"%lx", varReturnValue.lVal);

           OutputDebugString("set ip and mask address failed!!/n");

           OutputDebugString(a);

           goto END;

       }

       VariantClear(&varReturnValue);

       if (pOutParams)

       {

           pOutParams->Release();

           pOutParams = NULL;

       }

       if(pIpAddress)

       {

           SafeArrayDestroy(pIpAddress);

           pIpAddress = NULL;

       }

       if(pSubnetMask)

       {

           SafeArrayDestroy(pSubnetMask);

           pSubnetMask = NULL;

       }

       if (pInParams)

       {

           pInParams->Release();

           pInParams = NULL;

       }

       if (pInParamsDefinition)

       {

           pInParamsDefinition->Release();

           pInParamsDefinition = NULL;

       }

    }//ip and subnet mask address

 

    if (pData->ulStateMask & AS_GATEWAY_CHANGED)

    {

       if ((pData->ulStateMask & AS_IP_CHANGED) || (pData->ulStateMask & AS_MASK_CHANGED))

       {

           Sleep(5000);

       }

       DWORD dwGateway = htonl(pData->ulGateway);

       //Get the EnableStatic method and the in-params class.

       hres = g_pAdapterConfigClass->GetMethod(

           L"SetGateways",

           0,

           &pInParamsDefinition,

           NULL);

       if (FAILED(hres))

       {

           OutputDebugString("GetMethod failed!!/n");

           goto END;

       }

 

       //Get the in-params object.

       hres = pInParamsDefinition->SpawnInstance(0, &pInParams);

       if (FAILED(hres))

       {

           OutputDebugString("SpawnInstance failed!!/n");

           goto END;

       }

 

       //Set the in-params values for Getewat.

       bound[0].lLbound = 0;

       bound[0].cElements = 1;

       pGatewayAddress = SafeArrayCreate(VT_BSTR, 1, bound);

       if(pGatewayAddress == NULL)

       {

           OutputDebugString("SafeArrayCreate failed!!/n");

           goto END;

       }

       in.s_addr = dwGateway;

       //AnsiToUnicode(inet_ntoa(in),address);

       Gateway = inet_ntoa(in);

       OutputDebugString("the gateway address is:");

       OutputDebugStringW(Gateway);

       hres = SafeArrayPutElement(pGatewayAddress, &i, (BSTR)Gateway);

       if (FAILED(hres))

       {

           OutputDebugString("SafeArrayPutElement failed!!/n");

           goto END;

       }

 

       // bind the in-params.

       ArrayVal.parray = pGatewayAddress;

       ArrayVal.vt = VT_ARRAY|VT_BSTR;

       hres = pInParams->Put(L"DefaultIPGateway", 0,&ArrayVal, 0);

 

       // Now actually execute the "EnableStatic" method

       hres = g_pSvc->ExecMethod(

           g_AdapterConfigPathVal.bstrVal,

           L"SetGateways",

           0,

           NULL,

           pInParams,

           &pOutParams,

           NULL);

       if (FAILED(hres))

       {

           OutputDebugString("ExecMethod failed!!/n");

           goto END;

       }

// Get the return value.

 

       VariantInit(&varReturnValue);

       hres = pOutParams->Get(_bstr_t(L"ReturnValue"), 0, &varReturnValue, NULL, 0);

       if (FAILED(hres))

       {

           OutputDebugString("Get return value failed!!/n");

           goto END;

       }

 

       // Check the return value.

       if (0 != varReturnValue.lVal)

       {

           char a[100];

           sprintf(a,"%lx", varReturnValue.lVal);

           OutputDebugString(a);

           goto END;

       }

       VariantClear(&varReturnValue);

       if (pOutParams)

       {

           pOutParams->Release();

           pOutParams = NULL;

       }  

       if (pGatewayAddress)

       {

           SafeArrayDestroy(pGatewayAddress);

           pGatewayAddress = NULL;

       }

       if (pInParams)

       {

           pInParams->Release();

           pInParams = NULL;

       }

       if (pInParamsDefinition)

       {

           pInParamsDefinition->Release();

           pInParamsDefinition = NULL;

       }

    }//Gateway

 

    usStatus = AE_SUCCESS;

 

END:

    VariantClear(&varReturnValue);

    if (pOutParams)

    {

       pOutParams->Release();

       pOutParams = NULL;

    }

    if(pIpAddress)

    {

       SafeArrayDestroy(pIpAddress);

       pIpAddress = NULL;

    }

    if(pSubnetMask)

    {

       SafeArrayDestroy(pSubnetMask);

       pSubnetMask = NULL;

    }

    if (pGatewayAddress)

    {

       SafeArrayDestroy(pGatewayAddress);

       pGatewayAddress = NULL;

    }

    if (pInParams)

    {

       pInParams->Release();

       pInParams = NULL;

    }

    if (pInParamsDefinition)

    {

       pInParamsDefinition->Release();

       pInParamsDefinition = NULL;

    }

    ANWMIRelease();

    return usStatus;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值