采用修改注册表的方式实现:
在应用程序中修改IP地址的方式:
1 ////////////////////////////////////////////////////////////////////////// 2 // Set IP Address, Mask and Gateway 3 // Through the registry entry: 4 // [HKEY_LOCAL_MACHINE/Comm/DM9CE1/Parms/TcpIp] 5 // IpAddress, String 6 // SubnetMask, String 7 // DefaultGateway, String 8 ////////////////////////////////////////////////////////////////////////// 9 SYSTEMUTILITY_API BOOL SysUtil_IPConfig(LPCWSTR m_StrIPAddress, LPCWSTR m_StrIPMask, LPCWSTR m_StrGate) 10 { 11 long lResult = 0; 12 13 HANDLE hIPCEvent = CreateEvent(NULL, TRUE, FALSE, NULL); ; 14 LPCTSTR strSubKeyName = _T("Comm//DM9CE1//Parms//TcpIp"); 15 LPCTSTR strAdapterName = _T("DM9CE1"); 16 LPCTSTR strIP = _T("IpAddress"); 17 LPCTSTR strMask = _T("SubnetMask"); 18 LPCTSTR strGate = _T("DefaultGateway"); 19 LPCTSTR strDHCP = _T("EnableDHCP"); 20 DWORD dwValue = 0; 21 22 DWORD dwStatus = NotifyAddrChange(&hIPCEvent, NULL); 23 if(dwStatus != NO_ERROR) 24 return FALSE; 25 26 // Change IP Settings 27 HKEY hKey; 28 DWORD dwStrLen = 0; 29 if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKeyName, 0, 0, &hKey) == ERROR_SUCCESS) 30 { 31 dwStrLen = wcslen(m_StrIPAddress) * sizeof(WCHAR); 32 lResult = RegSetValueEx(hKey, strIP, 0, REG_SZ, (LPBYTE)m_StrIPAddress, dwStrLen); 33 if(lResult != ERROR_SUCCESS) 34 return FALSE; 35 36 dwStrLen = wcslen(m_StrIPMask) * sizeof(WCHAR); 37 lResult = RegSetValueEx(hKey, strMask, 0, REG_SZ, (LPBYTE)m_StrIPMask, dwStrLen); 38 if(lResult != ERROR_SUCCESS) 39 return FALSE; 40 41 dwStrLen = wcslen(m_StrGate) * sizeof(WCHAR); 42 lResult = RegSetValueEx(hKey, strGate, 0, REG_SZ, (LPBYTE)m_StrGate, dwStrLen); 43 if(lResult != ERROR_SUCCESS) 44 return FALSE; 45 46 dwStrLen = sizeof(dwValue); 47 lResult = RegSetValueEx(hKey, strDHCP, 0, REG_DWORD, (LPBYTE)&dwValue, dwStrLen); 48 if(lResult != ERROR_SUCCESS) 49 return FALSE; 50 51 RegCloseKey(hKey); 52 } 53 54 // Rebind NDIS 55 if(!RebindNdisAdapter(strAdapterName)) 56 return FALSE; 57 58 // Waiting until they are applied 59 if(WaitForSingleObject(hIPCEvent, WAIT_TIMEOUT_10S) != WAIT_OBJECT_0) 60 return FALSE; 61 62 CloseHandle(hIPCEvent); 63 64 return TRUE; 65 }