Click here to Skip to main content
Licence CPOL
First Posted 6 Jul 2004
Views 150,905
Downloads 2,213
Bookmarked 47 times

Programmatically Changing IP address, Domain Name Server and Gateways

By ThatsAlok | 15 Jul 2004
This article shows how we can change the IP address with corresponding subnet mask, gateway with corresponding metric, and DNS of local computer through programming.
9 votes, 30.0%
1
3 votes, 10.0%
2
4 votes, 13.3%
3
3 votes, 10.0%
4
11 votes, 36.7%
5
3.16/5 - 31 votes
μ 3.28, σa 3.00 [?]

Sample Image - testreg.gif

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.

  1. 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.

  2. 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:

Sample image

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);

  //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);

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.

//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);

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.

////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);

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)
 {
  //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;
 }

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

ThatsAlok

Software Developer

India India

Member
He used to have biography here Smile | :) , but now he will hire someone (for free offcourse Big Grin | :-D ), Who writes his biography on his behalf Smile | :)
 
He is Great Fan of Mr. Johan Rosengren (his idol),Lim Bio Liong (Great Programmer) , Nishant S(Is he need any introduction?) and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!
 

In October 2005, He joined the League of Visual C++ MVP Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questiona PinmemberMember 855191019:48 31 Jan '12  
Generalwin xp sp3 not working Pinmemberqaztrident7:07 16 Sep '09  
GeneralRe: win xp sp3 not working PinmemberRookie_Coder20:50 21 Sep '09  
GeneralMemory leaks PinmemberRandor19:27 21 Apr '07  
GeneralRe: Memory leaks PinmemberThatsAlok19:48 21 Apr '07  
GeneralThe WIN32 way: use IPHELPER Pinmemberahz5:53 17 Dec '05  
GeneralRe: The WIN32 way: use IPHELPER PinmemberRoland_W22:38 25 Jan '06  
Questionhow to really chane IP,Domain,Gateway.. Pinmemberiverboy1:01 30 Nov '05  
AnswerRe: how to really chane IP,Domain,Gateway.. PinmemberThatsAlok1:12 30 Nov '05  
GeneralRe: how to really chane IP,Domain,Gateway.. Pinmemberiverboy1:25 30 Nov '05  
GeneralRe: how to really chane IP,Domain,Gateway.. Pinmemberiverboy1:36 30 Nov '05  
AnswerRe: how to really chane IP,Domain,Gateway.. Pinmemberahz5:47 17 Dec '05  
AnswerRe: how to really chane IP,Domain,Gateway.. Pinmemberahz6:46 17 Dec '05  
AnswerRe: how to really chane IP,Domain,Gateway.. PinmemberFuxing18:13 23 Apr '06  
Questionthanks for your code! Pinsusscalvin200001:21 22 Oct '05  
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!
 


GeneralLAN traffic PinmemberVasil Nikolov23:12 25 Jul '05  
GeneralNot good PinmemberTeeBee3036:04 16 Jul '04  
GeneralRe: Not good PinmemberThatsAlok17:12 16 Jul '04  
GeneralRe: Not good PinmemberTeeBee30321:15 18 Jul '04  
GeneralRe: Not good PinmemberThatsAlok3:45 19 Jul '04  
QuestionRe: Not good Pinmemberiverboy8:33 30 Nov '05  
GeneralRe: Not good PinsussMsftone12:18 27 Jan '05  
GeneralRe: Not good PinmemberThatsAlok19:34 23 Mar '05  
QuestionChange between dynamic & static IP Adress? PinmemberRemco Overman7:16 13 Jul '04  
AnswerRe: Change between dynamic & static IP Adress? Pinmember ThatsAlok 20:51 13 Jul '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 16 Jul 2004
Article Copyright 2004 by ThatsAlok
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid