VC ++ directly changes the IP address without restarting windows

Source: Internet
Author: User

Address: http://www.cctry.com/thread-3653-1-1.html

The source code is run as follows:

Source code download:

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

  1. Ulong uladapterinfosize = sizeof (ip_adapter_info );
  2. Ip_adapter_info * padapterinfobkp, * padapterinfo = (ip_adapter_info *) New char [uladapterinfosize];
  3. If (getadaptersinfo (padapterinfo, & uladapterinfosize) = error_buffer_overflow) // the buffer is not large enough.
  4. {
  5. Delete padapterinfo;
  6. Padapterinfo = (ip_adapter_info *) New char [uladapterinfosize];
  7. Padapterinfobkp = padapterinfo;
  8. }
  9. If (getadaptersinfo (padapterinfo, & uladapterinfosize) = error_success)
  10. {
  11. Do {// traverse all adapters
  12. If (padapterinfo-> type = mib_if_type_ethernet) // determines whether the interface is an Ethernet interface.
  13. {
  14. // Padapterinfo-> description is the adapter description
  15. // Padapterinfo-> adaptername is the adapter name
  16. }
  17. Padapterinfo = padapterinfo-> next;
  18. } While (padapterinfo );
  19. }
  20. Delete padapterinfobkp;

Copy code

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:

  1. Hkey, hsubkey, hndiintkey;
  2. If (regopenkeyex (HKEY_LOCAL_MACHINE,
  3. "System \ CurrentControlSet \ Control \ class \ {4d36e972-e325-11ce-bfc1-08002be10318 }",
  4. 0,
  5. Key_read,
  6. & Hkey )! = Error_success)
  7. Return false;
  8. DWORD dwindex = 0;
  9. DWORD dwbufsize = 256;
  10. DWORD dwdatatype;
  11. Char szsubkey [256];
  12. Unsigned char szdata [256];
  13. While (regenumkeyex (hkey, dwindex ++, szsubkey, & dwbufsize, null) = error_success)
  14. {
  15. If (regopenkeyex (hkey, szsubkey, 0, key_read, & hsubkey) = error_success)
  16. {
  17. If (regopenkeyex (hsubkey, "NDI \ Interfaces", 0, key_read, & hndiintkey) = error_success)
  18. {
  19. Dwbufsize = 256;
  20. If (regqueryvalueex (hndiintkey, "lowerrange", 0, & dwdatatype, szdata, & dwbufsize) = error_success)
  21. {
  22. If (strcmp (char *) szdata, "Ethernet") = 0) // you can determine whether the network card is an Ethernet card.
  23. {
  24. Dwbufsize = 256;
  25. If (regqueryvalueex (hsubkey, "driverdesc", 0, & dwdatatype, szdata, & dwbufsize) = error_success)
  26. {
  27. // The detailed description of the adapter is in szdata.
  28. Dwbufsize = 256;
  29. If (regqueryvalueex (hsubkey, "netmask instanceid", 0, & dwdatatype, szdata, & dwbufsize) = error_success)
  30. {
  31. // The Name Of The adapter is in szdata.
  32. }
  33. }
  34. }
  35. }
  36. Regclosekey (hndiintkey );
  37. }
  38. Regclosekey (hsubkey );
  39. }
  40. Dwbufsize = 256;
  41. }/* End of while */
  42. Regclosekey (hkey );

Copy code

2. Write IP information to the Registry

The Code is as follows:

  1. Bool regsetip (maid, maid)
  2. {
  3. Hkey;
  4. String strkeyname = "System \ CurrentControlSet \ Services \ Tcpip \ Parameters \ Interfaces \\";
  5. Strkeyname + = lpszadaptername;
  6. If (regopenkeyex (HKEY_LOCAL_MACHINE,
  7. Strkeyname. c_str (),
  8. 0,
  9. Key_write,
  10. & Hkey )! = Error_success)
  11. Return false;
  12. Char mszipaddress [100];
  13. Char msznetmask [100];
  14. Char msznetgate [100];
  15. Strncpy (mszipaddress, pipaddress, 98 );
  16. Strncpy (msznetmask, pnetmask, 98 );
  17. Strncpy (msznetgate, pnetgate, 98 );
  18. Int NIP, nmask, ngate;
  19. Nip = strlen (mszipaddress );
  20. Nmask = strlen (msznetmask );
  21. Ngate = strlen (msznetgate );
  22. * (Mszipaddress + nip + 1) = 0x00; // The reg_multi_sz data must be followed by a value of 0.
  23. Nip + = 2;
  24. * (Msznetmask + nmask + 1) = 0x00;
  25. Nmask + = 2;
  26. * (Msznetgate + ngate + 1) = 0x00;
  27. Ngate + = 2;
  28. Regsetvalueex (hkey, "IPaddress", 0, reg_multi_sz, (unsigned char *) mszipaddress, nip );
  29. Regsetvalueex (hkey, "subnetmask", 0, reg_multi_sz, (unsigned char *) msznetmask, nmask );
  30. Regsetvalueex (hkey, "DefaultGateway", 0, reg_multi_sz, (unsigned char *) msznetgate, ngate );
  31. Regclosekey (hkey );
  32. Return true;
  33. }

Copy code

3. Call dhcpnotifyconfigchange to notify you of configuration changes.

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

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

Copy code

The specific call code is as follows:

  1. Bool notifyipchange (maid, int nindex, maid, maid)
  2. {
  3. Bool bresult = false;
  4. Hinstance hdhcpdll;
  5. Dhcpnotifyproc pdhcppolicyproc;
  6. Wchar wcadaptername [256];
  7. Multibytetowidechar (cp_acp, 0, lpszadaptername,-1, wcadaptername, 256 );
  8. If (hdhcpdll = loadlibrary ("dhcpcsvc") = NULL)
  9. Return false;
  10. If (pdhcppolicyproc = (dhcppolicyproc) getprocaddress (hdhcpdll, "dhcppolicyconfigchange "))! = NULL)
  11. If (pdhcppolicyproc) (null, wcadaptername, true, nindex, inet_addr (pipaddress), inet_addr (pnetmask), 0) = error_success)
  12. Bresult = true;
  13. Freelibrary (hdhcpdll );
  14. Return bresult;
  15. }

Copy code

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.