[Original documentation This article is suitable for intermediate readers have read 9571 times]
Author: Wenling, Zhejiang Province Wang Jun Telecommunications Note: This article is for WINDOWS NT / 2000 / XP / 2003 Download this article's sample project source operating results is as follows: Set the IP address only need to change corresponding registry settings for the adapter, but changes After need to restart the system to take effect, the addipaddress function can only add IP instead of changing the current IP, we can take effect on the Windows NT / 2000 interface, which can take effect, the system does what extra work has made IP settings directly take effect? Through tracking the call of the API in Explorer.exe, I found an unapproved API: DHCPNotifyConfigChange in Netcfgx.dll in Netcfgx.dll, and will not restart the detailed method of the Windows direct change IP address is described below: 1. Get adapter Name The adapter name referred to here is to distinguish between the adapter description, such as one of my network cards, adapter descriptions: Realtek RTL8139 (a) PCI Fast Ethernet Adapter, adapter name: {66156dc3-44a4-434c-b8a9-0DB4B3EEAD}.
Get the adapter name There are several ways: 1.1 call 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) // buffer is not large enough {delete pAdapterInfo; pAdapterInfo = (IP_ADAPTER_INFO *) new char [ulAdapterInfoSize]; pAdapterInfoBkp = pAdapterInfo;} if (GetAdaptersInfo (pAdapterInfo, & ulAdapterInfoSize) == ERROR_SUCCESS) {do {// iterate through all adapter if (pAdapterInfo-> Type == MIB_IF_TYPE_ETHERNET) // determines whether the Ethernet interface {// pAdapterInfo-> description adapter is described // pAdapterInfo-> AdapterName is adapter name} pAdapterInfo = pAdapterInfo-> Next;} while (pAdapterInfo );} delete pAdapterInfoBkp; read the registry to obtain the name of the adapter in Windows2000 by traversing HKEY_LOCAL_MACHINE / System / CurrentControlSet / Control / Class / {4d36e972-e325-11ce-bfc1-08002be10318} / 000n / (n 1.2 is zero Number) All interfaces, you can read the information in HKEY_LOCAL_MACHINE / CURRENTVERSION / NETWORKCAL_MACHINE / CURRENTVERSION / NETWORKCARDS in Windows NT, below: HKEY 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, NULL, NULL, 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, & dwdattype, szdata, & dwbufsize) == Error_Success) {if (strcmp ((char *) szdata, "ethernet") == 0) // Is it an Ethernet card {dwbufsize = 256; if (RegQueryValueEx (hSubKey, "DriverDesc", 0, & dwDataType, szData, & dwBufSize) == ERROR_SUCCESS) {// szData adapter is described in detail in dwBufSize = 256; if (RegQueryValueEx (hSubKey, "NetCfgInstanceID", 0 , & dwDataType, szData, & dwBufSize) == ERROR_SUCCESS) {// szData adapter is in the name}}}} RegCloseKey (hNdiIntKey);} RegCloseKey (hSubKey);} dwBufSize = 256;} / * end of while * / RegCloseKey ( hKey); two, the IP information of the code written to the registry as follows: BOOL RegSetIP (LPCTSTR lpszAdapterName, LPCTSTR pIPAddress, LPCTSTR pNetMask, LPCTSTR pNetGate) {HKEY 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, PIP Address, 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; // reg_multi_sz data needs to add 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;