BOOL SetIPAddr(char *ipaddr,char *netmask,char *gateway)
{
char *pAdaptername = GetAdapterName();
DWORD type = REG_MULTI_SZ;
HKEY hRoot = HKEY_LOCAL_MACHINE;
HKEY hKey;
char szSubKey[256];
DWORD des = REG_OPENED_EXISTING_KEY;
char buf[256];
sprintf(szSubKey, "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\%s",pAdaptername);
// 查找键值
LONG ret = RegCreateKeyExA(hRoot, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &des);
if(ret != ERROR_SUCCESS)
{
printf( "读取注册表失败!\n");
return(-1);
}
char mIpaddress[100];
char mGateway[100];
char mNetmask[100];
strncpy(mIpaddress, ipaddr, 98);
strncpy(mGateway, gateway, 98);
strncpy(mNetmask, netmask, 98);
int nIp = strlen(mIpaddress);
*(mIpaddress + nIp +1) = 0x00;
nIp += 2;
int nGateway = strlen(mGateway);
*(mGateway + nGateway +1) = 0x00;
nGateway += 2;
int nMask = strlen(mNetmask);
*(mNetmask + nMask +1) = 0x00;
nMask += 2;
// 设置ip地址
ret = RegSetValueEx(hKey, _TEXT("IPAddress"), 0, type, (PBYTE)mIpaddress, nIp);
if(ret != ERROR_SUCCESS)
{
printf("设置ip地址失败!\n");
return(-1);
}
// 设置网关
ret = RegSetValueEx(hKey, _TEXT("DefaultGateway"), 0, type, (PBYTE)mGateway, nGateway);
if(ret != ERROR_SUCCESS)
{
printf("设置网关失败!\n");
return(-1);
}
/*ret = RegSetValueEx(hKey,_TEXT("DhcpDefaultGateway"),0,type,(PBYTE)nGateway,nGateway);
if (ret != ERROR_SUCCESS)
{
printf("设置DNS网关失败!\n");
return(-1);
}*/
// 设置掩码
ret = RegSetValueEx(hKey, _TEXT("SubnetMask"), 0, type, (PBYTE)mNetmask, nMask);
if(ret != ERROR_SUCCESS)
{
printf("设置掩码失败!\n");
return FALSE;
}
if(isDhcp)
{
// 如果先前起用了dhcp 现在要禁用 必须设置dns
BYTE isd[4];
for(int uu=0; uu<4; uu++)
{
isd[uu] = 1;
}
ret = RegSetValueEx(hKey, _TEXT("EnableDHCP"), 0, REG_DWORD, (PBYTE)isd, sizeof(DWORD));
if(ret != ERROR_SUCCESS)
{
printf( "禁止dhcp失败!\n");
//return FALSE;
}
// 如果先前起用了dhcp 现在要禁用 必须设置dns
memset(dns,1,160);
ret = RegSetValueEx(hKey, _TEXT("NameServer"), 0, REG_SZ, (unsigned char*)dns, strlen(dns));
if(ret != ERROR_SUCCESS)
{
printf("设置dns失败!\n");
//return FALSE;
}
}
// 关闭注册表句柄
RegCloseKey(hKey);
if(RestNetCard(pAdaptername, ipaddr, netmask) == FALSE)
{
printf("Rest NetCard failed\n");
return FALSE;
}
return TRUE;
}
//通知系统重启网卡
BOOL RestNetCard(char* lpszAdapterName,char * pIPAddress,char* pNetMask)
{
HMODULE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
_bstr_t tempname =lpszAdapterName;
WCHAR *wcAdapterName = tempname;
LPCWSTR dhcp = _T("dhcpcsvc");
if((hDhcpDll = LoadLibrary(dhcp)) == NULL)
{
printf("连接到dhcpcsvc失败!");
return FALSE;
}
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
{
if(isDhcp)
{
isDhcp = FALSE;
// 禁止dhcp
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, 0, inet_addr(pIPAddress), inet_addr(pNetMask), 2) == ERROR_SUCCESS)
{
FreeLibrary(hDhcpDll);
return TRUE;
}
}
else
{
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, 0, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS)
{
FreeLibrary(hDhcpDll);
return TRUE;
}
}
}
return FALSE;
}
3 个解决方案
#1
我的情况是能改变了注册表中信息,但是无法通知系统,//-----------------------------------------------------------------
// 设置注册表中的IP信息
//-----------------------------------------------------------------
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 99);
strncpy(mszNetMask, pNetMask, 99);
strncpy(mszNetGate, pNetGate, 99);
int nIP, nMask, nGate;
nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
//-----------------------------------------------------------------
// 通知IP地址的改变
//-----------------------------------------------------------------
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
{
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];
MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);
hDhcpDll = LoadLibrary("dhcpcsvc.dll");
if(hDhcpDll == NULL)
{
::MessageBox(NULL, "Load dhcpcsvc.dll failed", "Error Info:", 0);
return FALSE;
}
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 2) == ERROR_SUCCESS)
bResult = TRUE;
FreeLibrary(hDhcpDll);
return bResult;
}
// 设置注册表中的IP信息
//-----------------------------------------------------------------
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 99);
strncpy(mszNetMask, pNetMask, 99);
strncpy(mszNetGate, pNetGate, 99);
int nIP, nMask, nGate;
nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
//-----------------------------------------------------------------
// 通知IP地址的改变
//-----------------------------------------------------------------
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
{
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];
MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);
hDhcpDll = LoadLibrary("dhcpcsvc.dll");
if(hDhcpDll == NULL)
{
::MessageBox(NULL, "Load dhcpcsvc.dll failed", "Error Info:", 0);
return FALSE;
}
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 2) == ERROR_SUCCESS)
bResult = TRUE;
FreeLibrary(hDhcpDll);
return bResult;
}
#2
不知道哪里出了问题,唉找了好长时间没解决,水平有限
#3
我也遇到了跟你一样的问题 估计是调用函数 DhcpNotifyConfigChange 时传的参数不对 你的解决了么?
#1
我的情况是能改变了注册表中信息,但是无法通知系统,//-----------------------------------------------------------------
// 设置注册表中的IP信息
//-----------------------------------------------------------------
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 99);
strncpy(mszNetMask, pNetMask, 99);
strncpy(mszNetGate, pNetGate, 99);
int nIP, nMask, nGate;
nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
//-----------------------------------------------------------------
// 通知IP地址的改变
//-----------------------------------------------------------------
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
{
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];
MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);
hDhcpDll = LoadLibrary("dhcpcsvc.dll");
if(hDhcpDll == NULL)
{
::MessageBox(NULL, "Load dhcpcsvc.dll failed", "Error Info:", 0);
return FALSE;
}
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 2) == ERROR_SUCCESS)
bResult = TRUE;
FreeLibrary(hDhcpDll);
return bResult;
}
// 设置注册表中的IP信息
//-----------------------------------------------------------------
BOOL RegSetIP(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate)
{
HKEY hKey;
string strKeyName = "SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\";
strKeyName += lpszAdapterName;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName.c_str(),
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
char mszIPAddress[100];
char mszNetMask[100];
char mszNetGate[100];
strncpy(mszIPAddress, pIPAddress, 99);
strncpy(mszNetMask, pNetMask, 99);
strncpy(mszNetGate, pNetGate, 99);
int nIP, nMask, nGate;
nIP = strlen(mszIPAddress);
nMask = strlen(mszNetMask);
nGate = strlen(mszNetGate);
*(mszIPAddress + nIP + 1) = 0x00;
nIP += 2;
*(mszNetMask + nMask + 1) = 0x00;
nMask += 2;
*(mszNetGate + nGate + 1) = 0x00;
nGate += 2;
RegSetValueEx(hKey, "IPAddress", 0, REG_MULTI_SZ, (unsigned char*)mszIPAddress, nIP);
RegSetValueEx(hKey, "SubnetMask", 0, REG_MULTI_SZ, (unsigned char*)mszNetMask, nMask);
RegSetValueEx(hKey, "DefaultGateway", 0, REG_MULTI_SZ, (unsigned char*)mszNetGate, nGate);
RegCloseKey(hKey);
return TRUE;
}
//-----------------------------------------------------------------
// 通知IP地址的改变
//-----------------------------------------------------------------
BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
{
BOOL bResult = FALSE;
HINSTANCE hDhcpDll;
DHCPNOTIFYPROC pDhcpNotifyProc;
WCHAR wcAdapterName[256];
MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);
hDhcpDll = LoadLibrary("dhcpcsvc.dll");
if(hDhcpDll == NULL)
{
::MessageBox(NULL, "Load dhcpcsvc.dll failed", "Error Info:", 0);
return FALSE;
}
if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)
if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 2) == ERROR_SUCCESS)
bResult = TRUE;
FreeLibrary(hDhcpDll);
return bResult;
}
#2
不知道哪里出了问题,唉找了好长时间没解决,水平有限
#3
我也遇到了跟你一样的问题 估计是调用函数 DhcpNotifyConfigChange 时传的参数不对 你的解决了么?