Change IP address & Mask address (C++) Windows

Introduction

This is one way of changing IP address & mask address in C++ using Iphlpapi.h. It’s a simple program, no UI for you, sorry…

  • Use GetAdaptersInfo to get adapters information.
  • Then use GetPerAdapterInfo to get information for every ethernet network card. The information contains IP address.
  • Use DeleteIPAddress remove the old IP address.
  • Use AddIPAddress to add new IP&Mask address.
// ChangeIPAddress("192.168.0.123", "192.168.0.44","255,255,255,0");
bool ChangeIPAddress(char oldIPAddress[], char newIPAddress[], char newMaskAddress[])
{
    DWORD dwRetVal = 0;
    PIP_ADAPTER_INFO pAdapter = NULL;

    ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
    PIP_ADAPTER_INFO pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
    if (pAdapterInfo == NULL)
    {
        return false;
    }

    if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW)
    {
        free(pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
        if (pAdapterInfo == NULL)
        {
            return false;
        }
    }

    if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR)
    {
        pAdapter = pAdapterInfo;
        while (pAdapter)
        {
            if (strcmp(oldIPAddress, pAdapter->IpAddressList.IpAddress.String) == 0)
            {
                IPAddr addr = inet_addr(newIPAddress);
                IPMask mask = inet_addr(newMaskAddress);
                ULONG context, instance;

                if (DeleteIPAddress(pAdapter->IpAddressList.Context) != NO_ERROR
                    || AddIPAddress(addr, mask, pAdapter->Index, &context, &instance) != NO_ERROR)
                {
                    return false;
                }
                return true;
            }
            pAdapter = pAdapter->Next;
        }
        return false;
    }
}

Windows change computer name without reboot

Save this as a batch file, and run from command console

set /p newName=Please input the new computer name:

echo Modifying registry keys...

reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName" /v "ComputerName" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName" /v "ComputerName" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet002\Control\ComputerName\ComputerName" /v "ComputerName" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName" /v "ComputerName" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\ComputerName\ComputerName" /v "ComputerName" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters" /v "Hostname" /d "%newName%" -f 
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters" /v "NV Hostname" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters" /v "Hostname" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services\Tcpip\Parameters" /v "NV Hostname" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "DefaultDomainName" /d "%newName%" -f
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon" /v "AltDefaultDomainName" /d "%newName%" -f

echo Finish updating registry...

echo Update DNS...
ipconfig /renew
ipconfig /flushdns
ipconfig /registerdns
echo Finish updating DNS...

set computername=%newName%