用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;

       }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值