用wmi來實現修改ip地址

來源:互聯網
上載者:User

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;

       }

    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.