http://www.vcchar.com/thread-1527-1-1.htmlhtml
設置IP地址只須要更改註冊表中關於適配器的相應設置,但更改後須要從新啓動系統才能生效,而AddIPAddress函數只能添加IP而不是更改當前的IP,咱們在Windows NT/2000界面上操做不須要從新啓動就能夠生效,那系統到底作了什麼額外的工做才使IP設置直接生效呢?筆者經過跟蹤explorer.exe中API的調用發如今netcfgx.dll中調用了dhcpcsvc.dll中一個未公開的API:DhcpNotifyConfigChange,現將 不從新啓動WINDOWS直接更改IP地址的詳細方法介紹以下ide
1、獲取適配器名稱函數
這裏指的適配器名稱要區別於適配器描述,好比個人一塊網卡,適配器描述是:Realtek RTL8139(A) PCI Fast Ethernet Adapter,適配器名稱爲:{66156DC3-44A4-434C-B8A9-0E5DB4B3EEAD}。獲取適配器名稱的方法有多種:spa
2、調用IP helper API取得適配器名稱
htm
- ULONG ulAdapterInfoSize = sizeof(IP_ADAPTER_INFO);
- IP_ADAPTER_INFO *pAdapterInfoBkp, *pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
- if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_BUFFER_OVERFLOW ) // 緩衝區不夠大
- {
- delete pAdapterInfo;
- pAdapterInfo = (IP_ADAPTER_INFO*)new char[ulAdapterInfoSize];
- pAdapterInfoBkp = pAdapterInfo;
- }
- if( GetAdaptersInfo(pAdapterInfo, &ulAdapterInfoSize) == ERROR_SUCCESS )
- {
- do{ // 遍歷全部適配器
- if(pAdapterInfo->Type == MIB_IF_TYPE_ETHERNET) // 判斷是否爲以太網接口
- {
- // pAdapterInfo->Description 是適配器描述
- // pAdapterInfo->AdapterName 是適配器名稱
- }
- pAdapterInfo = pAdapterInfo->Next;
- }while(pAdapterInfo);
- }
- delete pAdapterInfoBkp;
3、讀取註冊表取得適配器名稱接口
在Windows2000中能夠經過遍歷 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}\000n\ (n是從0開始編號的數字)全部接口, 在Windows NT中能夠讀取HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards中的信息,經過GUID找到相似ip
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\{8202c21f-87e8-4ed0-baeb-6441a7789ba9}原型
這樣的主鍵,下面以Windows2000爲例:string
- 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, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
- {
- if(strcmp((char*)szData, "ethernet") == 0) // 判斷是否是以太網卡
- {
- dwBufSize = 256;
- if(RegQueryValueEx(hSubKey, "DriverDesc", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
- {
- // szData 中即是適配器詳細描述
- dwBufSize = 256;
- if(RegQueryValueEx(hSubKey, "NetCfgInstanceID", 0, &dwDataType, szData, &dwBufSize) == ERROR_SUCCESS)
- {
- // szData 中即是適配器名稱
- }
- }
- }
- }
- RegCloseKey(hNdiIntKey);
- }
- RegCloseKey(hSubKey);
- }
-
- dwBufSize = 256;
- } /* end of while */
-
- RegCloseKey(hKey);
4、將IP信息寫入註冊表it
代碼以下
- 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, 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; // REG_MULTI_SZ數據須要在後面再加個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;
- }
5、調用DhcpNotifyConfigChange通知配置的改變
未公開函數DhcpNotifyConfigChange位於 dhcpcsvc.dll中,原型以下:
- BOOL DhcpNotifyConfigChange(
- LPWSTR lpwszServerName, // 本地機器爲NULL
- LPWSTR lpwszAdapterName, // 適配器名稱
- BOOL bNewIpAddress, // TRUE表示更改IP
- DWORD dwIpIndex, // 指明第幾個IP地址,若是隻有該接口只有一個IP地址則爲0
- DWORD dwIpAddress, // IP地址
- DWORD dwSubNetMask, // 子網掩碼
- int nDhcpAction ); // 對DHCP的操做 0:不修改, 1:啓用 DHCP,2:禁用 DHCP
具體調用代碼以下:
- BOOL NotifyIPChange(LPCTSTR lpszAdapterName, int nIndex, LPCTSTR pIPAddress, LPCTSTR pNetMask)
- {
- BOOL bResult = FALSE;
- HINSTANCE hDhcpDll;
- DHCPNOTIFYPROC pDhcpNotifyProc;
- 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;
- }