Modify the local IP address with C++ under windows

two methods

First, use DOS commands (immediate effect)

  

Second, modify the registry (restart to take effect)

1. Open SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards

  RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards",0,KEY_READ,&hKey)

2. Remove all subkeys under the open registry

  RegEnumKeyEx(hKey,dwIndex++,szSubKey,&dwBufSize,NULL,NULL,NULL,NULL)

3. Open the subkey

  RegOpenKeyEx(hKey,szSubKey,0,KEY_READ,&hSubKey)

4. Get the ServiceName of the network card

  RegQueryValueEx(hSubKey,"ServiceName",0,&dwDataType,szData,&dwBufSize)

5. Open the new registry according to the network card ServiceName

  RegOpenKeyEx(HKEY_LOCAL_MACHINE,“SYSTEM\\CurrentControlSet\\services\\”+szData+"\\Parameters\\Tcpip",0,KEY_ALL_ACCESS,&hLocalKey)

6. Modify the local network information

  //IP

  RegSetValueEx(hLocalKey,"IPAddress",0,type_ip,"192.168.123.123",dwIPSize)

  //SubnetMask

  RegSetValueEx(hLocalKey,"SubnetMask",0,type_SubnetMask,"255.255.255.0",dwSubnetMaskSize)

  //GateWay

  RegSetValueEx(hLocalKey,"DefaultGateway",0,type_GateWay,"192.168.12.1",dwGateWaySize)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326350979&siteId=291194637