Function ChangeIP(IP As String, NM As String, GW As String) As String 'If MsgBox("no to exit", vbYesNo) = vbNo Then End Dim strComputer, objWMIService, colNetAdapters, strIPAddress, strSubnetMask Dim strGateway, strGatewaymetric, objNetAdapter, errEnable, errGateways strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") strIPAddress = Array(IP) 'ip地址 strSubnetMask = Array(NM) '子网掩码 strGateway = Array(GW) '网关 strGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) If errEnable = 0 Then ChangeIP = "OK" Else ChangeIP = "FIN" End If NextEnd Function
Option Explicit Const REG_SZ As Long = 1 Const HKEY_LOCAL_MACHINE = &H80000002Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _ (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _ (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _ (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _ ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _ (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _ lpType As Long, lpData As Any, lpcbData As Long) As Long Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long Dim aaa As String Private Sub Command1_Click() Dim hKey As Long, hKey1 As Long, hKey2 As Long, ret As Long, ret1 As Long, lenData As Long, typeData As Long Dim Name As String, name1 As String, name2 As String, s As String, S1 As String Dim idx As Integer idx = 0 Name = String(256, Chr(0)) RegCreateKey HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Class\NetTrans", hKey Text1.Text = hKey Do ret = RegEnumKey(hKey, idx, Name, Len(Name)) If ret = 0 Then aaa = Left(Name, InStr(Name, Chr(0)) - 1) aaa = "System\CurrentControlSet\Services\Class\NetTrans\" & aaa ret = RegOpenKey(HKEY_LOCAL_MACHINE, aaa, hKey) '»ñµÃIPµØÖ· If ret = 0 Then name1 = "IPAddress" name2 = "IPMask" ret = RegQueryValueEx(hKey, name1, 0, typeData, ByVal vbNullString, lenData) s = String(lenData, Chr(0)) RegQueryValueEx hKey, name1, 0, typeData, ByVal s, lenData If s <> "" Then s = Left(s, InStr(s, Chr(0)) - 1) '»ñµÃ×ÓÍøÑÚÂë ret1 = RegQueryValueEx(hKey, name2, 0, typeData, ByVal vbNullString, lenData) S1 = String(lenData, Chr(0)) RegQueryValueEx hKey, name2, 0, typeData, ByVal S1, lenData If S1 <> "" Then S1 = Left(S1, InStr(S1, Chr(0)) - 1) If Val(s) > 0 Then Text1.Text = s Text2.Text = S1 Command2.Enabled = True Exit Sub End If End If idx = idx + 1 End If Loop Until ret <> 0 End Sub Private Sub Command2_Click() Dim hKey As Long If Text1.Text <> "" Then RegCreateKey HKEY_LOCAL_MACHINE, aaa, hKey ' ÐÞ¸ÄIPµØÖ· RegSetValueEx hKey, "IPAddress", 0, REG_SZ, ByVal Text1.Text, 13 ' ÐÞ¸Ä×ÓÍøÑÚÂë RegSetValueEx hKey, "IPMask", 0, REG_SZ, ByVal Text2.Text, 13 RegCloseKey hKey End If End SubPrivate Sub Command3_Click() End End SubPrivate Sub Form_Load()End Sub
zhujiechang(小朱) 那个是9x的。..改了注册表要正常使用要重启的.. 我想楼主问的是2000/XP/etc的改法吧.____ 我想WMI+API做的出来,可是我一点wmi都不懂..http://www.freevbcode.com/ShowCode.asp?ID=3083 这个是Internet Tools Suite Version 2.0 Author: Shane M. Croft (Featured Developer) Category: Network/Internet Type: Applications Difficulty: Advanced Version Compatibility: Visual Basic 6 More information: This application bundles a set of internet tools, including DNS lookup, SMTP e-mail, Trace Rout, Port Listener, Port Scanner and more. This is both an excellent example of advanced sockets programming and an excellent utility in its own right.里面有可以参考的代码哟,我也在想这个问题 嘿嘿。..
Function ChangeIP(IP As String, NM As String, GW As String, MDNS As String, SDNS As String) As String '返回值说明:返回一个设置的中文说明. Dim strComputer, objWMIService, colNetAdapters, strIPAddress, strSubnetMask Dim strGateway, strGatewaymetric, strDNS, objNetAdapter, errEnable, errGateways, errDNS strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") strIPAddress = Array(IP) 'ip地址 strSubnetMask = Array(NM) '子网掩码 strGateway = Array(GW) '网关 strDNS = Array(MDNS, SDNS) '主DNS各备DNS strGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNS) If errEnable = 0 And errGateways = 0 And errDNS = 0 Then ChangeIP = "设置成功" Else If errEnable = 0 Then ChangeIP = "IP地址和子网掩码设置成功, " Else ChangeIP = "IP地址或子网掩码设置失败, " End If If errGateways = 0 Then ChangeIP = ChangeIP & "默认网关设置成功, " Else ChangeIP = ChangeIP & "默认网关设置失败, " End If If errDNS = 0 Then ChangeIP = ChangeIP & "DNS设置成功" Else ChangeIP = ChangeIP & "DNS设置失败" End If End If Next End Function
'If MsgBox("no to exit", vbYesNo) = vbNo Then End
Dim strComputer, objWMIService, colNetAdapters, strIPAddress, strSubnetMask
Dim strGateway, strGatewaymetric, objNetAdapter, errEnable, errGateways
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array(IP) 'ip地址
strSubnetMask = Array(NM) '子网掩码
strGateway = Array(GW) '网关
strGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
If errEnable = 0 Then
ChangeIP = "OK"
Else
ChangeIP = "FIN"
End If
NextEnd Function
http://www.ccw.com.cn/htm/app/aprog/01_1_8_2.asp
Const REG_SZ As Long = 1
Const HKEY_LOCAL_MACHINE = &H80000002Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName As Long) As Long
Dim aaa As String
Private Sub Command1_Click()
Dim hKey As Long, hKey1 As Long, hKey2 As Long, ret As Long, ret1 As Long, lenData As Long, typeData As Long
Dim Name As String, name1 As String, name2 As String, s As String, S1 As String
Dim idx As Integer
idx = 0
Name = String(256, Chr(0))
RegCreateKey HKEY_LOCAL_MACHINE, "System\CurrentControlSet\Services\Class\NetTrans", hKey
Text1.Text = hKey
Do
ret = RegEnumKey(hKey, idx, Name, Len(Name))
If ret = 0 Then
aaa = Left(Name, InStr(Name, Chr(0)) - 1)
aaa = "System\CurrentControlSet\Services\Class\NetTrans\" & aaa
ret = RegOpenKey(HKEY_LOCAL_MACHINE, aaa, hKey)
'»ñµÃIPµØÖ·
If ret = 0 Then
name1 = "IPAddress"
name2 = "IPMask"
ret = RegQueryValueEx(hKey, name1, 0, typeData, ByVal vbNullString, lenData)
s = String(lenData, Chr(0))
RegQueryValueEx hKey, name1, 0, typeData, ByVal s, lenData
If s <> "" Then s = Left(s, InStr(s, Chr(0)) - 1)
'»ñµÃ×ÓÍøÑÚÂë
ret1 = RegQueryValueEx(hKey, name2, 0, typeData, ByVal vbNullString, lenData)
S1 = String(lenData, Chr(0))
RegQueryValueEx hKey, name2, 0, typeData, ByVal S1, lenData
If S1 <> "" Then S1 = Left(S1, InStr(S1, Chr(0)) - 1)
If Val(s) > 0 Then
Text1.Text = s
Text2.Text = S1
Command2.Enabled = True
Exit Sub
End If
End If
idx = idx + 1
End If
Loop Until ret <> 0
End Sub
Private Sub Command2_Click()
Dim hKey As Long
If Text1.Text <> "" Then
RegCreateKey HKEY_LOCAL_MACHINE, aaa, hKey
' ÐÞ¸ÄIPµØÖ·
RegSetValueEx hKey, "IPAddress", 0, REG_SZ, ByVal Text1.Text, 13
' ÐÞ¸Ä×ÓÍøÑÚÂë
RegSetValueEx hKey, "IPMask", 0, REG_SZ, ByVal Text2.Text, 13
RegCloseKey hKey
End If
End SubPrivate Sub Command3_Click()
End
End SubPrivate Sub Form_Load()End Sub
那个是9x的。..改了注册表要正常使用要重启的..
我想楼主问的是2000/XP/etc的改法吧.____
我想WMI+API做的出来,可是我一点wmi都不懂..http://www.freevbcode.com/ShowCode.asp?ID=3083
这个是Internet Tools Suite Version 2.0
Author: Shane M. Croft (Featured Developer)
Category: Network/Internet
Type: Applications
Difficulty: Advanced
Version Compatibility: Visual Basic 6
More information: This application bundles a set of internet tools, including DNS lookup, SMTP e-mail, Trace Rout, Port Listener, Port Scanner and more. This is both an excellent example of advanced sockets programming and an excellent utility in its own right.里面有可以参考的代码哟,我也在想这个问题 嘿嘿。..
http://community.csdn.net/Expert/topic/3595/3595157.xml?temp=.4939386
回复人: planetike(胜哥哥) ( ) 信誉:111 2005-05-10 13:05:00 得分: 0
Function ChangeIP(IP As String, NM As String, GW As String, MDNS As String, SDNS As String) As String
'返回值说明:返回一个设置的中文说明.
Dim strComputer, objWMIService, colNetAdapters, strIPAddress, strSubnetMask
Dim strGateway, strGatewaymetric, strDNS, objNetAdapter, errEnable, errGateways, errDNS
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array(IP) 'ip地址
strSubnetMask = Array(NM) '子网掩码
strGateway = Array(GW) '网关
strDNS = Array(MDNS, SDNS) '主DNS各备DNS
strGatewaymetric = Array(1)
For Each objNetAdapter In colNetAdapters
errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
errDNS = objNetAdapter.SetDNSServerSearchOrder(strDNS)
If errEnable = 0 And errGateways = 0 And errDNS = 0 Then
ChangeIP = "设置成功"
Else
If errEnable = 0 Then
ChangeIP = "IP地址和子网掩码设置成功, "
Else
ChangeIP = "IP地址或子网掩码设置失败, "
End If
If errGateways = 0 Then
ChangeIP = ChangeIP & "默认网关设置成功, "
Else
ChangeIP = ChangeIP & "默认网关设置失败, "
End If
If errDNS = 0 Then
ChangeIP = ChangeIP & "DNS设置成功"
Else
ChangeIP = ChangeIP & "DNS设置失败"
End If
End If
Next
End Function
用netsh这个命令http://community.csdn.net/Expert/topic/3996/3996985.xml?temp=.1029169