Introduction
Please forgive me for some bad English grammar I am going to use in this article, really I am very bad at English. Now, let me explain about this article.
This article helps you to change the IP address, DNS and gateway dynamically through programming.
Basically, the secret weapon for changing these are hidden in the registry. These two registry keys hold the key for that.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft \Windows\CurrentVersion\NetworkCards
Holds the information about all the cards installed on you computer in its subkeys that start from 1 to last.
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet \Services\TcpIp\Parameters
\Interfaces \%s
Here %s
is card name you got from above key.
Here is the screen shot for key #2 showing all the Regeditor structure:
Now I'll explain each and every function used in this project. Basically, the IP address and gateways are stored in registry in REG_MULTI_SZ
format, so I have created different functions for each type of network.
BOOL ViewNumberOfCard(CStringArray *arCard,
CStringArray *arCardName);
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);
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);
OK, now I'll explain to you the working of each function. Basically, each function's working is same but each is deal with different network identities.
BOOL ViewNumberOfCard(CStringArray *arCard,CStringArray *arCardName);
This function will return card number with the card name.
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);
Working of all functions are same. First parameter returns corresponding card number with its IP address except in ViewDNSSubnet
in which the third variable is used to retrieve the DNS from single user version.
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);
Working of these functions are also the same. They change the network identity of the computer.
Also, here is a small code for helping you view the IP address and to retrieve the value from the complicated REG_MULTI_SZ
format:
BOOL CNMPNetworkChange::ViewIPAddress(CStringArray &card,
CStringArray &ipaddress)
{
char *szValue=new char[600];
CString str;
DWORD pdw=599;
int i=0;
CRegKey key;
for(int flag=1;flag<=100;flag++)
{
szValue[0]=NULL;
pdw=599;
key.Close();
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;
RegQueryValueEx(key.m_hKey,
TEXT("IPAddress"),
NULL,
NULL,
(LPBYTE)szValue1,
&pdw);
char *temp=new char[20];
int j=0;
str.Format("%d",flag);
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;
}
I think I explained much. If the user encounters some problem while using my s/w, feel free to mail to me because while solving your problem, I'll learn something new.
Special thanks to my project team mates Mr. Rakesh Pant and Mr. Jatin Kumar.