VC Knowledge Base Article-change the IP address directly without restarting windows

Source: Internet
Author: User
Guidance:

Change the IP address directly without restarting windows
Author: Wang Jun, Wenling Telecom Bureau, Zhejiang Province

Note: This article applies to Windows NT/2000/XP/2003

Download the sample project in this article

The source code is run as follows:

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 (bytes); ip_adapter_info * bytes, * padapterinfo = (Bytes *) New char [uladapterinfosize]; If (getadaptersinfo (padapterinfo, & uladapterinfosize) = bytes) // The buffer is not large enough {Delete padapterinfo; padapterinfo = (ip_adapter_info *) New char [uladapterinfosize]; response = padapterinfo;} If (getadaptersinfo (padapterinfo, & uladapterinfosize) =) {do {// traverse all adapters if (padapterinfo-> type = mib_if_type_ethernet) // determine whether it 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 // {iterator}", 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) = signature) {dwbufsize = 256; If (regqueryvalueex (hndiintkey, "lowerrange", 0, & dwdatatype, szdata, & dwbufsize) = error_success) {If (strcmp (char *) szdata, "Ethernet") = 0) // determine if it is an Ethernet card {dwbufsize = 256; If (regqueryvalueex (hsubkey, "driverdesc", 0, & dwdatatype, szdata, & dwbufsize) = error_success) {// The detailed description of the adapter in szdata is dwbufsize = 256; If (regqueryvalueex (hsubkey, "netdeskinstanceid", 0, & dwdatatype, szdata, & dwbufsize) = error_success) in {// szdata, the adapter name is }}} regclosekey (hndiintkey);} regclosekey (hsubkey);} dwbufsize = 256 ;} /* end of while */regclosekey (hkey );

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; // Add 0nip + = 2 for reg_multi_sz data; * (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 *) mask, 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, // The adapter name bool bnewipaddress, // true indicates changing the IP address DWORD dwipindex, // specifies the IP address number, if there is only one IP address for this interface, it is 0 DWORD dwipaddress, // ip address DWORD dwsubnetmask, // subnet mask int ndhcpaction); // DHCP operation 0: not modified, 1: enable DHCP, 2: disable DHCP

The specific call code is as follows:

BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask){BOOLbResult = FALSE;HINSTANCEhDhcpDll;DHCPNOTIFYPROCpDhcpNotifyProc;WCHAR wcAdapterName[256];MultiByteToWideChar(CP_ACP, 0, lpszAdapterName, -1, wcAdapterName,256);if((hDhcpDll = LoadLibrary("dhcpcsvc")) == NULL)return FALSE;if((pDhcpNotifyProc = (DHCPNOTIFYPROC)GetProcAddress(hDhcpDll, "DhcpNotifyConfigChange")) != NULL)if((pDhcpNotifyProc)(NULL, wcAdapterName, TRUE, nIndex, inet_addr(pIPAddress), inet_addr(pNetMask), 0) == ERROR_SUCCESS)bResult = TRUE;FreeLibrary(hDhcpDll);return bResult;}

(Full text)
This article is transferred from
Http://www.vckbase.com/document/viewdoc? Id = 851

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.