
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.
| You must Sign In to use this message board. |
|
|
 |
|
 |
this wont work with winxp sp3
# HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft \Windows\CurrentVersion\NetworkCards key is absent
better use wmi to read network card id and extract it  System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
System.Net.NetworkInformation.NetworkInterface.Id property
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Was looking through your code and noticed a leak in the function ViewNumberOfCard() Once I find a leak I have a bad habit of looking for more. 
Found 2 more leaks in the function ViewDNSSubnet() when TRUE == (Ver==0)
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Randor wrote: Was looking through your code and noticed a leak in the function ViewNumberOfCard() Once I find a leak I have a bad habit of looking for more.
Thanks for looking into to code.. it's years since i looked back to these code.. could you please check or remove it for me.. i will republish it with your name.. thanks!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow cheers, Alok Gupta VC Forum Q&A :- I/ IVSupport CRY- Child Relief
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Yes although the IP Helper sometimes do not work as you think. For example it is impossible to delete an IP address from the table if your program didn't set it and have saved it's context. Use WMI instead.
|
| Sign In·View Thread·PermaLink | 3.67/5 |
|
|
|
 |
|
 |
Your code just can chang value in registry, but computer ip is not to be changed... how to change it.....
thanks...
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
iverboy wrote: Your code just can chang value in registry, but computer ip is not to be changed...
You have to restart your computer for that!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow cheers, Alok Gupta VC Forum Q&A :- I/ IV
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
thanks.. but why window ipconfig tool not to restart computer,ip can change.. and problem 2.. how to let DNS server address change automatically...
thanks very much...
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
sorry... I have another problem. how to know NIC really in computer... I use your code , but fine all NIC ever in PC.. How to filter them? I just want NIC in PC to be found...
Thanks...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
who have realized this problem ? May you post me source code or advicese by email? Thanks !
enjoy program
|
| Sign In·View Thread·PermaLink | 1.50/5 |
|
|
|
 |
|
 |
I think your code is very good ! and help me a lot !
but something I am not very understand clear . cause I use your code change IP several time , but when I want to get the current IP of system , than error occur !
I trace the program , found program stop at ViewIPAddress() function .I don't know why .I will try to find what is problem when I have time .but I want to know if you found this kind of error before ?
thanks!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hello,
I make my Diplomwork - through mobile phone to view the LAN traffic at a Server in real time and to change the networksettings. When you have any Ideas ...
thank you !
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
the way you are going to change the ip-address is not such a good idea, because:
- if a new windows is coming out, the names can change - to apply the changes you must restart the corresponding device driver. so you have to use the SetupDi... functions (windows DDK). - you didnt use a documented interface. instead, you reengineered it.
other ways to do it: - netsh.exe command (specially when you use batch files) - use wmi and the Win32_NetworkAdapterConfiguration class (this can also be done using vbscript) - use an answerfile to configure it (dunno how to do it, but there is something like this)
if you can use .net, i would use WMI. using WMI from .net or vbscript is really very easy (using WMI from C++ is a nightmare ^^).
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
sir,With All due Respect, TeeBee303 wrote: - if a new windows is coming out, the names can change
sorry i have tested this code on Window 2000,XP and 2003 and there it's working fine
TeeBee303 wrote: to apply the changes you must restart the corresponding device driver. so you have to use the SetupDi... functions (windows DDK).
Sir,i havn't restarted my computer to apply change till date,it start working as soon as i change the them.
TeeBee303 wrote: you didnt use a documented interface. instead, you reengineered it.
sir,as we all the programmer and CP is giving us the stage for showing there code,so we get critic and new idea to improve the code.
TeeBee303 wrote: use wmi and the Win32_NetworkAdapterConfiguration class ' really i don't know WMI work.
----------------------------- "I Think It will Work" Formerly I AM Known As "Alok The Programer" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
ThatsAlok wrote: sorry i have tested it on Window 2000,XP and 2003 and there is working fine you don't understand. that's not the way you should do things. always try to use documented APIs.
ThatsAlok wrote: Sir,i havn't restarted my computer to apply chnage till date,it start working as soon as i chnage them. i tested it. it is not working. the GUI (network IP properties) shows the new value applied to the registry, but it is not active. try ipconfig.exe. there is still the old value you have to notify the NICs driver that the settings have changed. you can do this using the following partial code:
. . . PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE; PropChangeParams.Scope = DICS_FLAG_GLOBAL; PropChangeParams.StateChange = DICS_PROPCHANGE ; if ( SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&PropChangeParams, sizeof(PropChangeParams)) == FALSE) { SetupDiDestroyDeviceInfoList (hDevInfo); return FALSE; }
if (SetupDiCallClassInstaller(DIF_PROPERTYCHANGE, hDevInfo, &DeviceInfoData)==FALSE) { SetupDiDestroyDeviceInfoList (hDevInfo); return FALSE; } . . .
or you can use the INetCfg COM interfaces. check out interface INetCfgComponentControl for method ApplyRegistryChanges()
ThatsAlok wrote: really i don't know WMI work. if it is your job to create system tools, you really should know WMI.
ThatsAlok wrote: thanks sir for reply ,i don't know why people is rating my article soo low,is it is not of any worth.i have just created a simple way to change the IPaddress etc of comp.any way hard luck look at your article and compare it to other good articles. check out your way of creating GUIs. they look terrible. sorry. the code in your apps is unformatted and doesn't look good. do you really think, that this article deserves a better rating?
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Yeah, i am agreed with you. Thanks for little partial code you given to me.i will update that in my article as soon as possible.
thank for posting reply , i really need these type of idea to improve my codes.
Thanks Again
----------------------------- "I Think It will Work" Formerly Known As "Alok The Programmer" at CP ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Deer Sir: I can't understand how to do.. I want know how to do that can not to restart computer and change registry immediately. can u post this part sourcecode on web...???
thanks..
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I also agree you should be using published APIs as registry locations change and also not a supported method of query if problems arise.
--- maximum 500 characters
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Sorry For replying Late!
Msftone wrote: I also agree you should be using published APIs as registry locations change and also not a supported method of query if problems arise. Yeah you are 100% correct, actually during my project,I find the way to change ipaddress, DNS server,etc through change of some key in registry. So i just published that only..
There are much better and documented way to change network Information using IPHELPER apis and WMI.
Thanks for replying, Comments are always USEFUL, it due to Mr. TEEBEE (the author of POST above you!), i came to know about WMI.I will try to include both in update of this article.
[Vote One Here, Complete my Survey....]
Alok Gupta visit me at http://www.thisisalok.tk
"I Think Believe this Will Help"
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi,
Thank for this cool code.
Is it possible improve the code that you can change between dynamic (dhcp) and static IP Adresses?
Thanks Remco
|
| Sign In·View Thread·PermaLink | 1.50/5 |
|
|
|
 |
|
 |
hi, thanks! for giving feedback. Remco Overman wrote: Is it possible improve the code that you can change between dynamic (dhcp) and static IP Adresses?
Really I am very-2 sorry, I am unable to understand what do u want, could u explain your problem please.
-----------------------------
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Sorry for replying so late i found how to change between the DHCP and Static IP
here is the key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"card mac"
you found Dword Value EnableDHCP if the value ==0 this mean the DHCP is OFF and if the value ==1 this mean the DHCP is ON
i will Update the code as early as possible
----------------------------- "I Think It Will Help" ----------------------------- Alok Gupta visit me at http://www.thisisalok.tk
|
| Sign In·View Thread·PermaLink | 3.50/5 |
|
|
|
 |
|
|