BOOL CIpSetDlg::RegSetIP(CString pIPAddress,CString pNetMask,CString pNetGate)//静态更改注册表信息
{
HKEY hKey;
CString strKeyName =L"COMM//PCI//R60401//Parms//TCPIP//";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName,
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
WCHAR mszIPAddress[100]={0};
WCHAR mszNetMask[100]={0};
WCHAR mszNetGate[100]={0};
wcsncpy(mszIPAddress,pIPAddress.GetBuffer(pIPAddress.GetLength()),pIPAddress.GetLength());
wcsncpy(mszNetMask,pNetMask.GetBuffer(pNetMask.GetLength()),pNetMask.GetLength());
wcsncpy(mszNetGate,pNetGate.GetBuffer(pNetGate.GetLength()),pNetGate.GetLength());
pIPAddress.ReleaseBuffer();
pNetMask.ReleaseBuffer();
pNetGate.ReleaseBuffer();
int nIP, nMask, nGate;
nIP = wcslen(mszIPAddress);
nMask = wcslen(mszNetMask);
nGate = wcslen(mszNetGate);
DWORD dhcpvalue=0x0;
RegSetValueEx(hKey,L"EnableDHCP", 0, REG_DWORD,(LPBYTE)&dhcpvalue,4);
//RegSetValueEx(hKey,L"DhcpIPAddress", 0, REG_MULTI_SZ,(LPBYTE)mszIPAddress,nIP*2);
RegSetValueEx(hKey,L"IPAddress", 0, REG_MULTI_SZ,(LPBYTE)mszIPAddress,nIP*2);
//RegSetValueEx(hKey,L"DhcpSubnetMask", 0, REG_MULTI_SZ,(LPBYTE)mszNetMask, nMask*2);
RegSetValueEx(hKey,L"SubnetMask", 0, REG_MULTI_SZ,(LPBYTE)mszNetMask, nMask*2);
//RegSetValueEx(hKey,L"DhcpDefaultGateway", 0, REG_MULTI_SZ,(LPBYTE)mszNetGate, nGate*2);
RegSetValueEx(hKey,L"DefaultGateway", 0, REG_MULTI_SZ,(LPBYTE)mszNetGate, nGate*2);
//RegFlushKey(hKey);
RegCloseKey(hKey);
return TRUE;
}
BOOL CIpSetDlg::RegAutoSetIP()//dhcp自动获得IP注册表设置
{
HKEY hKey;
CString strKeyName =L"COMM//PCI//R60401//Parms//TCPIP//";
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
strKeyName,
0,
KEY_WRITE,
&hKey) != ERROR_SUCCESS)
return FALSE;
DWORD dhcpvalue=0x1;
RegSetValueEx(hKey,L"EnableDHCP", 0, REG_DWORD,(LPBYTE)&dhcpvalue,4);
RegCloseKey(hKey);
return TRUE;
}
BOOL CIpSetDlg::SetNewDhcpIP()//动态捕获IP
{
UpdateData(true);
ULONG context;
ULONG Inst;
IPAddr NewIP;
IPAddr NewMask;
DWORD dwIfIndex;
char buf[256];
IP_ADAPTER_INDEX_MAP AdaptMap;
GetAdapterIndex(L"PCI//R60401",&dwIfIndex);
AdaptMap.Index=dwIfIndex;
wcscpy(AdaptMap.Name,L"PCI//R60401");
IpReleaseAddress(&AdaptMap);
DeleteIPAddress(AdaptMap.Index+2);//删除静态的IP
IpRenewAddress(&AdaptMap);
return true;
}
BOOL CIpSetDlg::SetNewIP()//静态设置IP加到列表
{
UpdateData(true);
ULONG context;
ULONG Inst;
IPAddr NewIP;
IPAddr NewMask;
DWORD dwIfIndex;
char buf[256];
IP_ADAPTER_INDEX_MAP AdaptMap;
GetAdapterIndex(L"PCI//R60401",&dwIfIndex);
AdaptMap.Index=dwIfIndex;
wcscpy(AdaptMap.Name,L"PCI//R60401");
memset(buf,0,256);
wcstombs(buf,m_ip,m_ip.GetLength());//宽字节转多字节
NewIP=inet_addr(buf);
memset(buf,0,256);
wcstombs(buf,m_netmask,m_netmask.GetLength());//宽字节转多字节
NewMask=inet_addr(buf);
IpReleaseAddress(&AdaptMap);
AddIPAddress(NewIP,NewMask,AdaptMap.Index,&context,&Inst);
DeleteIPAddress(context-1);//删除上一次的IP
//IpRenewAddress(&AdaptMap);
return true;
}
wince6下不重启改IP
最新推荐文章于 2020-12-23 20:16:25 发布
本文介绍了一个C++程序中用于静态和动态配置IP地址的方法。它包括通过注册表手动设置IP地址、子网掩码和默认网关的功能,以及启用DHCP自动获取IP地址的设置。此外,还提供了释放当前IP地址并重新获取新地址的功能。


4770

被折叠的 条评论
为什么被折叠?



