Multiple implementations of changing IP addresses without restarting windows

Source: Internet
Author: User
Tags nameserver

Address: http://www.vckbase.com/index.php/wv/645

Many netizens have encountered the problem of changing the IP address but restarting the machine. Here, we will introduce several methods to directly change the IP address without restarting windows. The first is to call the dhcppolicyconfigchange method, followed by the method to modify the registry and use "lplpapi.

  1. undisclosed functions: dhcppolicyconfigchange

Run the following command:

To set an IP address, you only need to change the corresponding settings for the adapter in the registry, but after the change, you need to restart the system to take effect. The addipaddress function can only add an IP address instead of changing the current IP address, the operation on the Windows NT/2000 interface can take effect without restarting. what additional work does the system do to make the IP settings take effect directly? Through tracking the API call in assumer.exe, the pen finds that an undisclosed API in dhcpcsvc. dll is called in netw.x. dll: dhcppolicyconfigchange. The detailed method for changing the IP address without restarting windows is described as follows:

  1. Get the adapter name

The name of the adapter must be different from the description of the adapter. For example, the name of an adapter is RealTek rtl8139 (a) PCI Fast Ethernet Adapter. The name of the adapter is {66156dc3-44a4-434c-b8a9-0e5db4b3eead }. You can use the following methods to obtain an adapter Name:

1.1 call the IP helper API to obtain the adapter name

2 Back to Top

Ulong uladapterinfosize = sizeof (ip_adapter_info );
Ip_adapter_info * padapterinfobkp, * padapterinfo = (ip_adapter_info *) New char [uladapterinfosize];
If (getadaptersinfo (padapterinfo, & uladapterinfosize) = error_buffer_overflow) // the buffer is not large enough.
{
Delete padapterinfo;
Padapterinfo = (ip_adapter_info *) New char [uladapterinfosize];
Padapterinfobkp = padapterinfo;
}
If (getadaptersinfo (padapterinfo, & uladapterinfosize) = error_success)
{
Do {// traverse all adapters
If (padapterinfo-> type = mib_if_type_ethernet) // determines whether the interface is an Ethernet interface.
{
// Padapterinfo-> description is the adapter description
// Padapterinfo-> adaptername is the adapter name
}
Padapterinfo = padapterinfo-> next;
} While (padapterinfo );
}
Delete padapterinfobkp;

1.2 read the registry and get the adapter name

In Windows2000, You can traverse all interfaces by HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ class \ {4d36e972-e325-11ce-bfc1-08002be10318} \ 000n \ (N is a number numbered from 0, in Windows NT, you can read information in HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ networkcards. The following uses Windows2000 as an example:

Hkey, hsubkey, hndiintkey;

If (regopenkeyex (HKEY_LOCAL_MACHINE,
"System \ CurrentControlSet \ Control \ class \ {4d36e972-e325-11ce-bfc1-08002be10318 }",
0,
Key_read,
& Hkey )! = Error_success)
Return false;

DWORD dwindex = 0;
DWORD dwbufsize = 256;
DWORD dwdatatype;
Char szsubkey [256];
Unsigned char szdata [256];

While (regenumkeyex (hkey, dwindex ++, szsubkey, & dwbufsize, null) = error_success)
{
If (regopenkeyex (hkey, szsubkey, 0, key_read, & hsubkey) = error_success)
{
If (regopenkeyex (hsubkey, "NDI \ Interfaces", 0, key_read, & hndiintkey) = error_success)
{
Dwbufsize = 256;
If (regqueryvalueex (hndiintkey, "lowerrange", 0, & dwdatatype, szdata, & dwbufsize) = error_success)
{
If (strcmp (char *) szdata, "Ethernet") = 0) // you can determine whether the network card is an Ethernet card.
{
Dwbufsize = 256;
If (regqueryvalueex (hsubkey, "driverdesc", 0, & dwdatatype, szdata, & dwbufsize) = error_success)
{
// The detailed description of the adapter is in szdata.
Dwbufsize = 256;
If (regqueryvalueex (hsubkey, "netmask instanceid", 0, & dwdatatype, szdata, & dwbufsize) = error_success)
{
// The Name Of The adapter is in szdata.
}
}
}
}
Regclosekey (hndiintkey );
}
Regclosekey (hsubkey );
}

Dwbufsize = 256;
}/* End of while */

Regclosekey (hkey );

3 Back to Top

  2. Write IP information to the Registry

The Code is as follows:

Bool regsetip (maid, maid)
{
Hkey;
String strkeyname = "System \ CurrentControlSet \ Services \ Tcpip \ Parameters \ Interfaces \\";
Strkeyname + = lpszadaptername;
If (regopenkeyex (HKEY_LOCAL_MACHINE,
Strkeyname. c_str (),
0,
Key_write,
& Hkey )! = Error_success)
Return false;

Char mszipaddress [100];
Char msznetmask [100];
Char msznetgate [100];

Strncpy (mszipaddress, pipaddress, 98 );
Strncpy (msznetmask, pnetmask, 98 );
Strncpy (msznetgate, pnetgate, 98 );

Int NIP, nmask, ngate;

Nip = strlen (mszipaddress );
Nmask = strlen (msznetmask );
Ngate = strlen (msznetgate );

* (Mszipaddress + nip + 1) = 0x00; // The reg_multi_sz data must be followed by a value of 0.
Nip + = 2;

* (Msznetmask + nmask + 1) = 0x00;
Nmask + = 2;

* (Msznetgate + ngate + 1) = 0x00;
Ngate + = 2;

Regsetvalueex (hkey, "IPaddress", 0, reg_multi_sz, (unsigned char *) mszipaddress, nip );
Regsetvalueex (hkey, "subnetmask", 0, reg_multi_sz, (unsigned char *) msznetmask, nmask );
Regsetvalueex (hkey, "DefaultGateway", 0, reg_multi_sz, (unsigned char *) msznetgate, ngate );

Regclosekey (hkey );

Return true;
}

  3. Call dhcpnotifyconfigchange to notify you of configuration changes.

The undisclosed function dhcppolicyconfigchange is located in dhcpcsvc. dll. The prototype is as follows:

Bool dhcppolicyconfigchange (
Lpwstr lpwszservername, // the local machine is null
Lpwstr lpwszadaptername, // adapter name
Bool bnewipaddress, // true indicates changing the IP address
DWORD dwipindex, // specifies the IP addresses. If only one IP address is available for this interface, the value is 0.
DWORD dwipaddress, // ip address
DWORD dwsubnetmask, // Subnet Mask
Int ndhcpaction); // DHCP action 0: do not modify, 1: enable DHCP, 2: disable DHCP

The specific call code is as follows:

Bool notifyipchange (maid, int nindex, maid, maid)
{
Bool bresult = false;
Hinstance hdhcpdll;
Dhcpnotifyproc pdhcppolicyproc;
Wchar wcadaptername [256];

Multibytetowidechar (cp_acp, 0, lpszadaptername,-1, wcadaptername, 256 );

If (hdhcpdll = loadlibrary ("dhcpcsvc") = NULL)
Return false;

If (pdhcppolicyproc = (dhcppolicyproc) getprocaddress (hdhcpdll, "dhcppolicyconfigchange "))! = NULL)
If (pdhcppolicyproc) (null, wcadaptername, true, nindex, inet_addr (pipaddress), inet_addr (pnetmask), 0) = error_success)
Bresult = true;

Freelibrary (hdhcpdll );
Return bresult;
}

4 Back to Top

  2. Modify the Registry: restart the NIC

Change the IP address in the Windows Nic attribute options. By comparing the front and back registries, you can find the following changes:

[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services
\ Tcpip \ Parameters \ Interfaces \ {97efdad8-eb2d-4f40-9b07-0fcd706fcb6d}]
"IPaddress"
"Subnetmask"
"DefaultGateway"
"Nameserver"

[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ {97efdad8-eb2d-4f40-9b07-0fcd706fcb6d} \ Parameters \ Tcpip]
"IPaddress"
"Subnetmask"
"DefaultGateway"

[HKEY_LOCAL_MACHINE \ SYSTEM \ controlset001 \ Services \ Tcpip \ Parameters \ Interfaces \ {97efdad8-eb2d-4f40-9b07-0fcd706fcb6d}]
"IPaddress"
"Subnetmask"
"DefaultGateway"
"Nameserver"

[HKEY_LOCAL_MACHINE \ SYSTEM \ controlset001 \ Services \ {97efdad8-eb2d-4f40-9b07-0fcd706fcb6d} \ Parameters \ Tcpip]
"IPaddress"
"Subnetmask"
"DefaultGateway"

Among them, {97efdad8-eb2d-4f40-9b07-0fcd706fcb6d} is the network adapter name (adaptername). Different network adapters, different access locations, and different access time have different values, the value is the guid value generated by the system when the system is connected for the first time.

CurrentControlSet is actually the alias of controlset001.

[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \
Tcpip \ Parameters \ Interfaces \ {97efdad8-eb2d-4f40-9b07-0fcd706fcb6d}]
"IPaddress"
"Subnetmask"
"DefaultGateway"
"Nameserver"

Is the main setting.

[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Services \ {97efdad8-eb2d-4f40-9b07-0fcd706fcb6d} \ Parameters \ Tcpip]
"IPaddress"
"Subnetmask"
"DefaultGateway"

Some services may be affected. If this parameter is not set, you can use netstat to see that the original IP address is still in the listening status (?).

However, to make the settings take effect, another important step is to restart the NIC. 5 back to the top.

To change the NIC configuration, You need to restart the NIC, such as Linux.

# Ifconfig eth0 down
# Ifconfig eht0 up

You can restart the NIC.

In Windows, the steps are similar: Disable local connection (NIC) and then enable local connection (NIC ). but there is no corresponding command or direct API. fortunately, DDK provides a set of device installation functions to control system devices, including device status changes. (Click to view the code attachment)

Conclusion: There are two steps to change the IP address by restarting the NIC: Modify the registry and restart the NIC. the whole process of restarting the NIC is described above. the Registry Modification content lists four main items in this article. For example, the NIC name of {97efdad8-eb2d-4f40-9b07-0fcd706fcb6d} is the internal device name, which is provided in the adapter structure. the entire registry modification process is relatively simple, which is not described in this article.

  3. Use "iphlpapi" with one card and multiple IP addresses

In addition to the above two methods, I will introduce another method. A network card can have multiple IP addresses at the same time, either in Windows or Linux. according to the TCP/IP principle, the network layer identifies the communication node as an IP address, while the link layer identifies as a MAC address. as long as multiple IP addresses are mapped to One Mac through ARP, a single network card with multiple IP addresses (actually One Mac with multiple IP addresses) can be implemented ). the system also has corresponding setting options. For example, if windows is added through the advanced options of TCP/IP properties, the ifconfig command can be added in Linux.

The lplpapi provides addipaddress and delipaddress. If you can add a new IP address and then remove the original IP address, you can change the IP address.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.