Click here to Skip to main content
11,429,777 members (77,859 online)
Click here to Skip to main content

Programmatically Changing IP address, Domain Name Server and Gateways

, 15 Jul 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

ThatsAlok
Software Developer (Senior)
India India
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, Nishant S and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!


For good 8 years he was Visual CPP MSMVP!

Comments and Discussions

 
Questionchange ip address without unplug data card in java Pin
Pintu Ranjan Sai6-Jul-13 11:32
memberPintu Ranjan Sai6-Jul-13 11:32 
AnswerRe: change ip address without unplug data card in java Pin
ThatsAlok11-Jul-13 21:40
memberThatsAlok11-Jul-13 21:40 
Questiona Pin
Member 855191031-Jan-12 19:48
memberMember 855191031-Jan-12 19:48 
This is what I always use amd no problems so far http://howtohacklife101.blogspot.com/2012/01/how-to-change-your-ip-address.html
AnswerRe: a Pin
ThatsAlok11-Jul-13 21:42
memberThatsAlok11-Jul-13 21:42 
Generalwin xp sp3 not working Pin
qaztrident16-Sep-09 7:07
memberqaztrident16-Sep-09 7:07 
GeneralRe: win xp sp3 not working Pin
Rookie_Coder21-Sep-09 20:50
memberRookie_Coder21-Sep-09 20:50 
GeneralMemory leaks Pin
Randor21-Apr-07 19:27
memberRandor21-Apr-07 19:27 
GeneralRe: Memory leaks Pin
ThatsAlok21-Apr-07 19:48
memberThatsAlok21-Apr-07 19:48 
GeneralThe WIN32 way: use IPHELPER Pin
ahz17-Dec-05 5:53
memberahz17-Dec-05 5:53 
GeneralRe: The WIN32 way: use IPHELPER Pin
Roland_W25-Jan-06 22:38
memberRoland_W25-Jan-06 22:38 
Questionhow to really chane IP,Domain,Gateway.. Pin
iverboy30-Nov-05 1:01
memberiverboy30-Nov-05 1:01 
AnswerRe: how to really chane IP,Domain,Gateway.. Pin
ThatsAlok30-Nov-05 1:12
memberThatsAlok30-Nov-05 1:12 
GeneralRe: how to really chane IP,Domain,Gateway.. Pin
iverboy30-Nov-05 1:25
memberiverboy30-Nov-05 1:25 
GeneralRe: how to really chane IP,Domain,Gateway.. Pin
iverboy30-Nov-05 1:36
memberiverboy30-Nov-05 1:36 
AnswerRe: how to really chane IP,Domain,Gateway.. Pin
ahz17-Dec-05 5:47
memberahz17-Dec-05 5:47 
AnswerRe: how to really chane IP,Domain,Gateway.. Pin
ahz17-Dec-05 6:46
memberahz17-Dec-05 6:46 
AnswerRe: how to really chane IP,Domain,Gateway.. Pin
Fuxing23-Apr-06 18:13
memberFuxing23-Apr-06 18:13 
Questionthanks for your code! Pin
calvin2000022-Oct-05 1:21
susscalvin2000022-Oct-05 1:21 
GeneralLAN traffic Pin
Vasil Nikolov25-Jul-05 23:12
memberVasil Nikolov25-Jul-05 23:12 
GeneralNot good Pin
TeeBee30316-Jul-04 6:04
memberTeeBee30316-Jul-04 6:04 
GeneralRe: Not good Pin
ThatsAlok16-Jul-04 17:12
memberThatsAlok16-Jul-04 17:12 
GeneralRe: Not good Pin
TeeBee30318-Jul-04 21:15
memberTeeBee30318-Jul-04 21:15 
GeneralRe: Not good Pin
ThatsAlok19-Jul-04 3:45
memberThatsAlok19-Jul-04 3:45 
QuestionRe: Not good Pin
iverboy30-Nov-05 8:33
memberiverboy30-Nov-05 8:33 
GeneralRe: Not good Pin
Msftone27-Jan-05 12:18
sussMsftone27-Jan-05 12:18 
GeneralRe: Not good Pin
ThatsAlok23-Mar-05 19:34
memberThatsAlok23-Mar-05 19:34 
QuestionChange between dynamic &amp; static IP Adress? Pin
Remco Overman13-Jul-04 7:16
memberRemco Overman13-Jul-04 7:16 
AnswerRe: Change between dynamic &amp; static IP Adress? Pin
ThatsAlok 13-Jul-04 20:51
member ThatsAlok 13-Jul-04 20:51 
AnswerRe: Change between dynamic &amp; static IP Adress? Pin
ThatsAlok25-Sep-04 4:30
memberThatsAlok25-Sep-04 4:30 
QuestionWin95/98/Me ? Pin
Dimitris Vassiliades7-Jul-04 11:06
memberDimitris Vassiliades7-Jul-04 11:06 
AnswerRe: Win95/98/Me ? Pin
Alok the programmer7-Jul-04 21:43
memberAlok the programmer7-Jul-04 21:43 
GeneralRe: Win95/98/Me ? Pin
Tal Halamish16-Mar-05 5:00
sussTal Halamish16-Mar-05 5:00 
GeneralRe: Win95/98/Me ? [modified] Pin
ThatsAlok 23-Mar-05 19:24
member ThatsAlok 23-Mar-05 19:24 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 16 Jul 2004
Article Copyright 2004 by ThatsAlok
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid