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
- 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;
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:
- 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 );
Copy code
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;
- }
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:
- 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
Copy code
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;
- }
Copy code