IP Address Swapping | CodeGuru

IP Address Swapping

Environment: VC 6, NT4 This is an application used to swap the IP address. For connecting to a computer in different domain the user has to change the IP address through the Network properties, for this the user has to follow 3 to 4 steps. In this application, the IP address of your computer will […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 14, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: VC 6, NT4

This is an application used to swap the IP address. For connecting to a computer in different domain the user has to change the IP address through the Network properties, for this the user has to follow 3 to 4 steps.

In this application, the IP address of your computer will be displayed in the listbox,the user can double click it to set it as the current IP address.

  • This application can be used only in Windows NT 4.0
  • The user should have the administrator privilege

This is a trial version for swapping the IP address in your computer.
Later version will be updated frequently

Warning:This application uses the registry for manipulating the IP address.

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// IPswapDlg.cpp : implementation file
#include “stdafx.h”
#include “IPswap.h”
#include “IPswapDlg.h”
BOOL CIPswapDlg::OnInitDialog()
{
  IPList=(CListBox*)GetDlgItem(IDC_IPADDLST);
  HKEY AppKey = NULL;
  LONG lRet1 = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
     “SYSTEM\\CurrentControlSet\\Services\\NetBT\\Linkage”,
     0,
     KEY_EXECUTE|KEY_READ|KEY_ALL_ACCESS,
     &AppKey);
  DWORD dwS=MAXPATH;
  unsigned char szPath[MAXPATH];
  if(RegQueryValueEx(AppKey,”Bind”,NULL,NULL,szPath,&dwS))
  {
    AfxMessageBox(“Error Accessing Registry”);
  }
  NetAdapt=(CString)szPath;
  NetAdapt.MakeReverse();
  int slash=NetAdapt.Find(“\\”,0);
  NetAdapt=NetAdapt.Left(slash);
  NetAdapt.MakeReverse();
  IPList->ResetContent();
  HKEY AppPathKey = NULL;
  LONG lRet = RegOpenKeyEx(
     HKEY_LOCAL_MACHINE,
     “SYSTEM\\CurrentControlSet\\Services\\”+
            NetAdapt+”\\Parameters\\Tcpip”,
     0,
     KEY_EXECUTE|KEY_READ|KEY_ALL_ACCESS,
     &AppPathKey);
  DWORD dwSize = MAXPATH;
  DWORD dwSize1 = MAXPATH;
  unsigned char szAppPath[MAXPATH];
  unsigned char szAppPath1[MAXPATH];
  CString FullIP,SubnetMask;
  if( RegQueryValueEx( AppPathKey,
                       “IpAddress”,
                       NULL,
                       NULL,
                       szAppPath,
                       &dwSize)!= ERROR_SUCCESS )
  {
     AfxMessageBox(“Error while Accessing registry”);
     return 0;
  }
  else
  {
     for(int i=0;i<255;i++)
       if((CString)szAppPath[i]!=””)
       FullIP=FullIP+(CString)szAppPath[i];
       else
       {
          if(FullIP.GetLength()>=8)
          {
            IPList->AddString(FullIP);
          }
          FullIP=””;
       }
  }
  if(RegQueryValueEx( AppPathKey,
                      “SubnetMask”,
                      NULL,
                      NULL,
                      szAppPath1,
                      &dwSize1)!= ERROR_SUCCESS )
  {
     AfxMessageBox(“Error while Accessing registry”);
     return 0;
  }
  else{
     for(int i=0;i<255;i++)
       if((CString)szAppPath1[i]!=””)
       SubnetMask=SubnetMask+(CString)szAppPath1[i];
       else
       {
         if(SubnetMask.GetLength()>=8)
         {
         subnetarray.Add(SubnetMask);
         }
         SubnetMask=””;
       }
  }
  RegCloseKey(AppPathKey);
  return TRUE;  // return TRUE  unless you set
                // the focus to a control
}
void CIPswapDlg::OnDblclkIpaddlst()
{
  CString teststr,teststr1;
  CString iptobemoved=iparray.GetAt(0);
  CString subnettobemoved=subnetarray.GetAt(0);
  int curr=IPList->GetCurSel();
  if(curr==0) return;
  teststr=iparray.GetAt(curr);
  teststr1=subnetarray.GetAt(curr);
  iparray.SetAt(curr,iptobemoved);
  iparray.SetAt(0,teststr);
  subnetarray.SetAt(curr,subnettobemoved);
  subnetarray.SetAt(0,teststr1);
  IPList->ResetContent();
  HKEY AppPathKey = NULL;
  LONG lRet = RegOpenKeyEx(  HKEY_LOCAL_MACHINE,
                “SYSTEM\\CurrentControlSet\\Services\\”+
                NetAdapt+”\\Parameters\\Tcpip”,
                0,
                KEY_EXECUTE|KEY_READ|KEY_ALL_ACCESS,
                &AppPathKey);
  DWORD dwSize = MAXPATH;
  unsigned char szAppPath[MAXPATH];
  CString FullIP;
  if(RegQueryValueEx( AppPathKey,
                      “IpAddress”,
                      NULL,
                      NULL,
                      szAppPath,
                      &dwSize)!= ERROR_SUCCESS )
  {
     AfxMessageBox(“Error while Accessing registry”);
     return;
  }
  int z=0;
  unsigned char ii[100];
  unsigned char subne[100];
  CString ip,sn;
  for(int p=0;p<=iparray.GetSize()-1;p++)
  {
     ip=iparray.GetAt(p);
     for(int k=0;k<=ip.GetLength()-1;k++)
     {
       ii[z]=ip.GetAt(k);
       z=z+1;
     }
     ii[z]=’\0′;
     z++;
  }
  int x=0;
  for(int q=0;q<=subnetarray.GetSize()-1;q++)
  {
     sn=subnetarray.GetAt(q);
     for(int k=0;k<=sn.GetLength()-1;k++)
     {
       subne[x]=sn.GetAt(k);
       x=x+1;
     }
     subne[x]=’\0′;
     x++;
  }
  RegSetValueEx(AppPathKey,”IpAddress”,0,REG_MULTI_SZ,ii,z);
  RegSetValueEx(AppPathKey,”SubnetMask”,0,REG_MULTI_SZ,subne,x);
  RegCloseKey(AppPathKey);
  for(int j=0;j<=iparray.GetSize()-1;j++)
  {
     IPList->AddString(iparray.GetAt(j));
  }
     MessageBox(“Successfully swaped”);
}

Downloads

Download demo project – 9Kb

Download source – 22Kb

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.