Three methods for modifying IP addresses without restarting the machine program in Windows

Source: Internet
Author: User
Tags nameserver

Three methods for IP address modification in Windows

The Platform discussed below is based on Windows XP + SP1 and does not consider compatibility issues of other Windows versions. However, it is theoretically common for NT systems.

Method 1: Nic restart
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.

To change the NIC configuration, restart the NIC, as shown in figure
In Linux, you only need to run
# 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.

/*************************************** **************************************** *********
Purpose: change state of the selected Device
Input: hdevinfo device info set
Pdeviceinfodata selected device info
Newstate one of enable/disable
Output: true for success, false for failed
**************************************** **************************************** ********/
Bool changedevicestate (hdevinfo, psp_devinfo_data pdeviceinfodata, DWORD newstate)
{
Sp_propchange_params propchangeparams = {sizeof (sp_classinstall_header )};
Sp_devinstall_params devparams;

If (! Pdeviceinfodata ){
Return false;
}

Propchangeparams. classinstallheader. cbsize = sizeof (sp_classinstall_header );
Propchangeparams. classinstallheader. installfunction = dif_propertychange;
Propchangeparams. Scope = dics_flag_configspecific;
Propchangeparams. statechange = newstate;
Propchangeparams. hwprofile = 0;

If (! Setupdisetclassinstallparams (hdevinfo, pdeviceinfodata,
(Sp_classinstall_header *) & propchangeparams, sizeof (propchangeparams ))
|! Setupdicallclassinstaller (dif_propertychange, hdevinfo, pdeviceinfodata )){
Return false;
}
Reutrn true;
}

/* How does hdevinfo get *********************************** ************************/
M_hdevinfo = setupdigetclassdevs (
(Lpguid) & guid_devclass_net,/* guid_devclass_net indicates to list only network devices */
Null,
This-> m_hwnd,
Digcf_present );
If (invalid_handle_value = m_hdevinfo ){
Return false;
}

/* How to obtain pdeviceinfodata *********************************** ***************/
K = 0;
While (setupdienumdeviceinfo (m_hdevinfo, K, & deviceinfodata )){
K ++;
If (cr_success! = Cm_get_devnode_status (& status, & problem,
Deviceinfodata. devinst, 0 )){
Continue;
}
If (Status & dn_no_show_in_dm )){
Continue;
}
If (getregistryproperty (m_hdevinfo,
& Deviceinfodata,
Spdrp_friendlyname,
& Pbuffer,
& Length )){
M_adapter [adapter_num]. Index = k-1;/* index of the current Nic In the device information set */
_ Tcscpy (m_adapter [adapter_num]. DESC, pbuffer);/* Current Nic */
Getregistryproperty (m_hdevinfo,
& Deviceinfodata,
Spdrp_driver,
& Pbuffer,
& Length );
_ Tcscpy (m_adapter [adapter_num]. Driver, pbuffer );
Adapter_num ++;
}
}

/* Getregistryproperty encapsulates ***************/
Bool getregistryproperty (hdevinfo deviceinfoset,
Psp_devinfo_data deviceinfodata,
Ulong property,
Lptstr * buffer,
Pulong length)
{
While (! Setupdigetdeviceregistryproperty (
Deviceinfoset,
Deviceinfodata,
Property,
Null,
(Pbyte) (* buffer ),
* Length,
Length
))
{
If (getlasterror () = error_insufficient_buffer ){
If (* (lptstr *) buffer)
Localfree (* (lptstr *) buffer );
* Buffer = (lptstr) localalloc (lptr, * length );
} Else {
Return false;
}
}
Return true;
}

/* M_adapter Data Structure */
Typedef struct adapter_info_s {
Char name [name_len];/* internal device name, in the uuid string format */
Char DESC [name_len];/* Nic Description */
Char driver [name_len];/* the location of the NIC in the registry, for example, {4d36e972-e325-11ce-bfc1-08002be10318}/0011
The actual complete key name is system // CurrentControlSet // control // class/{4d36e972-e325-11ce-bfc1-08002be10318}/0011.
The content of this key is basically the same as that of the device property obtained by setupdigetdeviceregistryproperty.
For example, netdeskinstanceid indicates that the internal device name driverdesc indicates the device Description */
Int index;
} Adapter_info_t;

/*************************************** **************************************
What is the name used to differentiate different NICs?
The following names are available.
Local Connection name. This is the method used by the system. netman is called. the undisclosed function hrlanconnectionnamefromguidorpath in DLL (the prototype is being debugged and will be written separately after the prototype is successful ); you can also obtain HKLM/system/CurrentControlSet/control/Network/{4d36e972-e325-11ce-bfc1-08002be10318}/{97efdad8-eb2d-4f40-9b07-0fcd706fcb6d}/connection/Name
Nic type description
The device friendly name is basically the same as the network card type description. If there is a network card of the same type, it will add # n (n = 2, 3, 4...) to distinguish
In this program, I will distinguish the network card by the friendly name of the device.
**************************************** *************************************/

/* Restart the NIC ********************************** **************************/
K = padapter-> getcursel ();/* m_adapter [k] is the current Nic */
If (setupdienumdeviceinfo (m_hdevinfo, m_adapter [K]. index, & deviceinfodata ))
{
Hcursor = setcursor (loadcursor (null, idc_wait ));
Changedevicestate (m_hdevinfo, & deviceinfodata, dics_disable);/* disable Nic */
Changedevicestate (m_hdevinfo, & deviceinfodata, dics_enable);/* enable Nic */
/* Restart the NIC. Generally, the following command is equivalent to the combination of the first two commands. However, we recommend that you use the former command */
// Changedevicestate (m_hdevinfo, & deviceinfodata, dics_propchange );
Setcursor (hcursor );
}

/* Tail scanning */
Setupdidestroydeviceinfolist (m_hdevinfo );

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.

Method 2: undisclosed Functions

In Windows, you can change the IP address in the Windows Nic attribute option to make the change take effect immediately without restarting the NIC. the netsh provided by the system can also be used in the form of command line or script files. When you change the IP address, you do not need to restart the NIC.
There are also a lot of shared software that can achieve the same function. Common IP address change software include ipfreeset, ipchanger, ipprofile, iphelp, ipset, snet, etc.

The author analyzes netsh and finds an undisclosed function, that is, the dhcppolicyconfigchange function in dhcpcsvc. dll encapsulated by netw.x. dll.
For more information about the specific method, see "change the IP address directly without restarting windows" by Mr. Wang of vckb in phase 25. His function prototype is more accurate than me and his idea is clearer.

When analyzing the above shared software, we found that there are three technical points: Using undisclosed functions, calling netsh commands, and restarting network card hardware. The essence of calling netsh commands is still using undisclosed functions.

Some undisclosed functions are used: ipfreeset and ipchanger.
The netsh commands are called: iphelp and ipset. Both are developed using Delphi.
Restart Nic hardware: ipswitcher

Speed comparison: Because netsh itself calls netmask X. DLL, netmask X. DLL encapsulates the use of undisclosed functions, so the efficiency is relatively low. in a system with a CPU: piii500/Ram: 256/XP, the iPhone P needs 6 ~ In 7 seconds, ipfreeset only needs 1 ~ 2 seconds.

Method 3: One Card with 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.
For more information, see my next article "iphlpapi.

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.