通过编程方式更改 IP 地址、域名服务器和网关
本文档展示了如何通过编程方式更改本地计算机的 IP 地址(以及相应的子网掩码)、网关(以及相应的度量值)和 DNS。
引言
请原谅我文章中可能存在的糟糕的英语语法,我真的不擅长英语。现在,让我解释一下本文的内容。
本文档帮助您通过编程方式动态更改 IP 地址、DNS 和网关。
基本上,更改这些设置的秘诀隐藏在注册表中。以下两个注册表键掌握了关键:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft \Windows\CurrentVersion\NetworkCards
该键包含计算机上所有已安装网卡的子键信息,子键从 1 开始,直到最后一个。
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet \Services\TcpIp\Parameters
\Interfaces \%s
这里 %s
是从上述键中获取的网卡名称。
这是显示所有 Regeditor 结构的键 #2 的屏幕截图
现在我将解释项目中使用的每个函数。基本上,IP 地址和网关以 REG_MULTI_SZ
格式存储在注册表中,因此我为每种类型的网络创建了不同的函数。
BOOL ViewNumberOfCard(CStringArray *arCard, CStringArray *arCardName); //this function send the current status in program BOOL ViewGateway(CStringArray &card,CStringArray &ipaddress); void ViewGateWayMetrics(CStringArray &card,CStringArray &ipaddress); BOOL ViewSubnetMask(CStringArray &card,CStringArray &ipaddress); BOOL ViewDNSSubnet (CStringArray &card,CStringArray &ipaddress,int var); BOOL ViewIPAddress (CStringArray &card,CStringArray &ipaddress); ////function change the cuurnet status BOOL ChangeGateway(CString card,CStringArray &ipaddress); BOOL ChangeGateWayMetrics(CString card,CStringArray &ipaddress); BOOL ChangeDNSSubnet (CString card,CStringArray &ipaddress,int ver); BOOL ChangeSubnetMask(CString card,CStringArray &ipaddress); BOOL ChangeIpAddress (CString card,CStringArray &ipaddress);
好的,现在我将向您解释每个函数的运作方式。基本上,每个函数的运作方式相同,但每个函数处理不同的网络标识。
BOOL ViewNumberOfCard(CStringArray *arCard,CStringArray *arCardName);
此函数将返回带有网卡名称的网卡编号。
//this function send the current status in program BOOL ViewGateway(CStringArray &card,CStringArray &ipaddress); void ViewGateWayMetrics(CStringArray &card,CStringArray &ipaddress); BOOL ViewSubnetMask(CStringArray &card,CStringArray &ipaddress); BOOL ViewDNSSubnet (CStringArray &card,CStringArray &ipaddress,int var); BOOL ViewIPAddress (CStringArray &card,CStringArray &ipaddress);
所有函数的工作方式相同。第一个参数返回带有其 IP 地址的相应网卡编号,除了 ViewDNSSubnet
,其中第三个变量用于从单用户版本中检索 DNS。
////function change the current status BOOL ChangeGateway(CString card,CStringArray &ipaddress); BOOL ChangeGateWayMetrics(CString card,CStringArray &ipaddress); BOOL ChangeDNSSubnet (CString card,CStringArray &ipaddress,int ver); BOOL ChangeSubnetMask(CString card,CStringArray &ipaddress); BOOL ChangeIpAddress (CString card,CStringArray &ipaddress);
这些函数的工作方式也相同。它们更改计算机的网络标识。
此外,这里有一小段代码可以帮助您查看 IP 地址并从复杂的 REG_MULTI_SZ
格式中检索值
BOOL CNMPNetworkChange::ViewIPAddress(CStringArray &card, CStringArray &ipaddress) { //declare some useful variable char *szValue=new char[600]; CString str; DWORD pdw=599; int i=0;
//registry variable come from header file ATLBASE.h CRegKey key; for(int flag=1;flag<=100;flag++) { szValue[0]=NULL; pdw=599; key.Close(); //this flag variable check number of network card in computer str.Format("SOFTWARE\\Microsoft\\Windows NT \\CurrentVersion\\NetworkCards\\%d",flag); if(key.Open(HKEY_LOCAL_MACHINE,str,KEY_READ)==ERROR_SUCCESS) { key.QueryValue(szValue,"ServiceName",&pdw); key.Close(); str.Format("SYSTEM\\CurrentControlSet\\Services \\TcpIp\\Parameters\\Interfaces\\%s",szValue); if(key.Open(HKEY_LOCAL_MACHINE,str,KEY_READ)!=ERROR_SUCCESS) { } char *szValue1=new char[2000]; pdw=1999; //querry the REG_MULTI_SZ value RegQueryValueEx(key.m_hKey, TEXT("IPAddress"), NULL, NULL, (LPBYTE)szValue1, &pdw); char *temp=new char[20]; int j=0; str.Format("%d",flag);
//NOw this is logic of retriving the value from the REG_MULTI_SZ for(i=0;i<(int)pdw;i++) { if(szValue1[i]!=NULL) { temp[j++]=szValue1[i]; } else { temp[j]=NULL; if(strcmp(temp,"")!=0) { card.Add(str); ipaddress.Add(temp); } j=0; } } delete[] temp; delete[] szValue1; key.Close(); } } delete[] szValue; return TRUE; }
我想我已经解释得很清楚了。如果用户在使用我的软件时遇到任何问题,请随时给我发邮件,因为在解决您的问题时,我也会学到一些新东西。
特别感谢我的项目团队成员 Rakesh Pant 先生和 Jatin Kumar 先生。