DWORD dwAddress=192<<24|168<<16|1<<8|168;
m_IPAddress.SetAddress(dwAddress);
DWORD dwNetMask=0xffffff00;
m_NetMask.SetAddress(dwNetMask);
DWORD dwNetGate=192<<24|168<<16|1<<8|1;
m_NetGate.SetAddress(dwNetGate);
/********************************************************************/
/*二、将IP信息写入注册表
代码如下:*/
BOOL RegSetIP(LPCTSTR lpszAdapterName, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM//CurrentControlSet//Services//Tcpip//Parameters//Interfaces//";
strKeyName += (char*)lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
(LPCSTR)strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, (LPCSTR)pIPAddress, 98);
strncpy(mszNetMask, (LPCSTR)pNetMask, 98);
strncpy(mszNetGate, (LPCSTR)pNetGate, 98);
int nIP, nMask, nGate;
nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00; // REG_MULTI_SZ数据需要在后面再加个0
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, (LPCSTR)"IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, (LPCSTR)"SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey,(LPCSTR)"DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
/*三、调用DhcpNotifyConfigChange通知配置的改变
未公开函数DhcpNotifyConfigChange位于 dhcpcsvc.dll中,原型如下: BOOL DhcpNotifyConfigChange(
LPWSTR lpwszServerName, // 本地机器为NULL
LPWSTR lpwszAdapterName, // 适配器名称
BOOL bNewIpAddress, // TRUE表示更改IP
DWORD dwIpIndex, // 指明第几个IP地址,如果只有该接口只有一个IP地址则为0
DWORD dwIpAddress, // IP地址
DWORD dwSubNetMask, // 子网掩码
int nDhcpAction ); // 对DHCP的操作 0:不修改, 1:启用 DHCP,2:禁用 DHCP
具体调用代码如下:*/
typedef BOOL (CALLBACK* DHCPNOTIFYPROC)(LPWSTR, LPWSTR, BOOL, DWORD, DWORD, DWORD, int);
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
{
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];
MultiByteToWideChar(CP_ACP, 0, (LPCSTR)lpszAdapterName, -1, wcAdapterName,256);
if((hDhcpDll = LoadLibrary("dhcpcsvc")) == NULL)
return FALSE;
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr((LPCSTR)pIPAddress), inet_addr((LPCSTR)pNetMask), 0) == ERROR_SUCCESS)
bResult = TRUE;
FreeLibrary(hDhcpDll);
return bResult;
}
void CIPchangeDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
byte dwAddress[4];
m_IPAddress.GetAddress(dwAddress[0],dwAddress[1],dwAddress[2],dwAddress[3]);
byte dwNetMask[4];
m_NetMask.GetAddress(dwNetMask[0],dwNetMask[1],dwNetMask[2],dwNetMask[3]);
byte dwNetGate[4];
m_NetGate.GetAddress(dwNetGate[0],dwNetGate[1],dwNetGate[2],dwNetGate[3]);
/*一、获取适配器名称
这里指的适配器名称要区别于适配器描述,比如我的一块网卡,适配器描述是:Realtek RTL8139(A) PCI Fast Ethernet Adapter,适配器名称为:{66156DC3-44A4-434C-B8A9-0E5DB4B3EEAD}。获取适配器名称的方法有多种:
/*1.2 读取注册表取得适配器名称
在Windows2000中可以通过遍历 HKEY_LOCAL_MACHINE/System/CurrentControlSet/Control/Class/{4d36e972-e325-11ce-bfc1-08002be10318}/000n/ (n是从0开始编号的数字)所有接口, 在Windows NT中可以读取HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVersion/NetworkCards中的信息,下面以Windows2000为例: HKEY hKey, hSubKey, hNdiIntKey;
*/
HKEY hKey;
HKEY hSubKey;
HKEY hNdiIntKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"System//CurrentControlSet//Control//Class//{4d36e972-e325-11ce-bfc1-08002be10318}",
0,
KEY_READ,
(HKEY*)&hKey) != ERROR_SUCCESS)
return ;
DWORD dwIndex = 0;
DWORD dwBufSize = 256;
DWORD dwDataType;
char szSubKey[256];
unsigned char szData[256];
while(RegEnumKeyEx((HKEY)hKey, dwIndex++, szSubKey, &dwBufSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
if(RegOpenKeyEx((HKEY)hKey, szSubKey, 0, KEY_READ, (PHKEY)&hSubKey) == ERROR_SUCCESS)
{
if(RegOpenKeyEx((HKEY)hSubKey, "Ndi//Interfaces", 0, KEY_READ, (PHKEY)&hNdiIntKey) == ERROR_SUCCESS)
{
dwBufSize = 256;
if(RegQueryValueEx((HKEY)hNdiIntKey, "LowerRange", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
if(strcmp((char*)szData, "ethernet") == 0) // 判断是不是以太网卡
{
dwBufSize = 256;
if(RegQueryValueEx((HKEY)hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
// szData 中便是适配器详细描述
dwBufSize = 256;
if(RegQueryValueEx((HKEY)hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
{
// szData 中便是适配器名称
string CAddress=inet_ntoa(*(in_addr*)&dwAddress);
string CNetMask=inet_ntoa(*(in_addr*)&dwNetMask);
string CNetGate=inet_ntoa(*(in_addr*)&dwNetGate);
BOOL boolreg=RegSetIP((char*)szData,CAddress.c_str(),CNetMask.c_str(),CNetGate.c_str());
BOOL notbool=NotifyIPChange((char*)szData,0,CAddress.c_str(),CNetMask.c_str());
}
}
}
}
RegCloseKey((HKEY)hNdiIntKey);
}
RegCloseKey((HKEY)hSubKey);
}
dwBufSize = 256;
} /* end of while */
RegCloseKey((HKEY)hKey);
MessageBox("修改完成!");
}