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;
}
}
#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;
}
}