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 Private Sub Command1_Click() If ChangeIP("192.168.1.12", "255.255.255.0", "192.168.1.1") = "OK" Then MsgBox "ok" Else MsgBox "no" End If End Sub
'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
Private Sub Command1_Click()
If ChangeIP("192.168.1.12", "255.255.255.0", "192.168.1.1") = "OK" Then
MsgBox "ok"
Else
MsgBox "no"
End If
End Sub
可不可以分享你的代码