VC directly modifies the IP address (no need to restart)

Source: Internet
Author: User

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? By following 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:

2. Call the IP helper API to obtain the adapter name

Ulong uladapterinfosize = sizeof (ip_adapter_info); <br/> ip_adapter_info * handle, * padapterinfo = (optional *) New char [uladapterinfosize]; <br/> If (getadaptersinfo (padapterinfo, & uladapterinfosize) = error_buffer_overflow) // the buffer is not large enough <br/> {<br/> Delete padapterinfo; <br/> padapterinfo = (ip_adapter_info *) New char [uladapterinfosize]; <br/> padapterinfobkp = padapterinfo; <br/>}< br/> If (getadaptersinfo (padapterinfo, & uladapterinfosize) = error_success) <br/>{< br/> do {// traverse all adapters <br/> If (padapterinfo-> type = mib_if_type_ethernet) // determine whether an Ethernet interface is used <br/>{< br/> // padapterinfo-> description is the adapter description <br/> // padapterinfo-> adaptername is the adapter name <br />}< br/> padapterinfo = padapterinfo-> next; <br/>}while (padapterinfo); <br/>}< br/> Delete padapterinfobkp;

3. Read the registry to obtain 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; </P> <p> If (regopenkeyex (HKEY_LOCAL_MACHINE, <br/> "system // CurrentControlSet // control // class // {4d36e972-e325-11ce-bfc1-08002be10318}", <br/> 0, <br/> key_read, <br/> & hkey )! = Error_success) <br/> return false; </P> <p> DWORD dwindex = 0; <br/> DWORD dwbufsize = 256; <br/> DWORD dwdatatype; <br/> char szsubkey [256]; <br/> unsigned char szdata [256]; </P> <p> while (regenumkeyex (hkey, dwindex ++, szsubkey, & dwbufsize, null) = error_success) <br/> {<br/> If (regopenkeyex (hkey, szsubkey, 0, key_read, & hsubkey) = error_success) <br/>{< br/> If (regopenkeyex (hsubkey, "NDI // interfaces", 0, key_read, & hndiintkey) = error_success) <br/>{< br/> dwbufsize = 256; <br/> If (regqueryvalueex (hndiintkey, "lowerrange", 0, & dwdatatype, szdata, & dwbufsize) = error_success) <br/>{< br/> If (strcmp (char *) szdata, "Ethernet") = 0) // determine whether it is an Ethernet Card <br/>{< br/> dwbufsize = 256; <br/> If (regqueryvalueex (hsubkey, "driverdesc", 0, & dwdatatype, szdata, & dwbufsize) = error_success) <br/>{< br/> // detailed description of the adapter in szdata <br/> dwbufsize = 256; <br/> If (regqueryvalueex (hsubkey, "netmask instanceid", 0, & dwdatatype, szdata, & dwbufsize) = error_success) <br/>{< br/> // The Name Of The adapter is in szdata <br/>}< br/> regclosekey (hndiintkey ); <br/>}< br/> regclosekey (hsubkey); <br/>}</P> <p> dwbufsize = 256; <br/>}/ * end of while */</P> <p> regclosekey (hkey );

4. Write IP information to the Registry

The Code is as follows:

Bool regsetip (maid, maid) <br/>{< br/> hkey; <br/> string strkeyname = "system // CurrentControlSet // services // TCPIP // parameters // interfaces //"; <br/> strkeyname + = lpszadaptername; <br/> If (regopenkeyex (HKEY_LOCAL_MACHINE, <br/> strkeyname. c_str (), <br/> 0, <br/> key_write, <br/> & hkey )! = Error_success) <br/> return false; </P> <p> char mszipaddress [100]; <br/> char msznetmask [100]; <br/> char msznetgate [100]; </P> <p> strncpy (mszipaddress, pipaddress, 98); <br/> strncpy (msznetmask, pnetmask, 98 ); <br/> strncpy (msznetgate, pnetgate, 98); </P> <p> int NIP, nmask, ngate; </P> <p> nip = strlen (mszipaddress); <br/> nmask = strlen (msznetmask); <br/> ngate = strlen (msznetgate ); </P> <p> * (mszipaddress + nip + 1) = 0x00; // Add 0 to the end of reg_multi_sz data <br/> nip + = 2; </P> <p> * (msznetmask + nmask + 1) = 0x00; <br/> nmask + = 2; </P> <p> * (msznetgate + ngate + 1) = 0x00; <br/> ngate + = 2; </P> <p> regsetvalueex (hkey, "IPaddress", 0, reg_multi_sz, (unsigned char *) mszipaddress, nip); <br/> regsetvalueex (hkey, "subnetmask", 0, reg_multi_sz, (unsigned char *) msznetmask, nmask); <br/> regsetvalueex (hkey, "DefaultGateway", 0, reg_multi_sz, (unsigned char *) msznetgate, ngate ); </P> <p> regclosekey (hkey); </P> <p> return true; <br/>}

5. Call dhcpnotifyconfigchange to notify you of configuration changes.

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

Bool dhcpnotifyconfigchange (<br/> lpwstr lpwszservername, // the local machine is null <br/> lpwstr lpwszadaptername, // adapter name <br/> bool bnewipaddress, // true indicates changing the IP address <br/> DWORD dwipindex, // specifies the IP addresses. If only one IP address is available for this interface, the value 0 <br/> DWORD dwipaddress, // ip address <br/> DWORD dwsubnetmask, // subnet mask <br/> int ndhcpaction); // DHCP 0: do not modify, 1: enable DHCP, 2: disable DHCP <br/>

The specific call code is as follows:

Bool policyipchange (maid, int nindex, maid, maid) <br/>{< br/> boolbresult = false; <br/> condition; <br/> wchar wcadaptername [256]; </P> <p> multibytetowidechar (cp_acp, 0, lpszadaptername,-1, wcadaptername, 256 ); </P> <p> If (hdhcpdll = loadlibrary ("dhcpcsvc") = NULL) <br/> return false; </P> <p> If (pdhcppolicyproc = (d Hcppolicyproc) getprocaddress (hdhcpdll, "dhcppolicyconfigchange "))! = NULL) <br/> If (pdhcppolicyproc) (null, wcadaptername, true, nindex, inet_addr (pipaddress), inet_addr (pnetmask), 0) = error_success) <br/> bresult = true; </P> <p> freelibrary (hdhcpdll); <br/> return bresult; <br/>}

6. program running effect:

VII ,:

Http://www.hellocpp.net/Common/Download.aspx? Attachment_id = 226

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.