'用WMI来获得主机的IP等 '窗体部分 Dim gintNetworkAdapter As LongPrivate Namespace As SWbemServices Private Method As SWbemMethod 'Default System Address Const SystemAddress = "127.0.0.0" Private Sub cboAdapter_Click() 'ClearAll Controls Call clearall 'Get Configuration for the selected adapter Call GetIPConfig(cboAdapter.ItemData(cboAdapter.ListIndex)) 'Set a Global Variable for the selected adapter gintNetworkAdapter = cboAdapter.ItemData(cboAdapter.ListIndex) End SubPrivate Sub cmdChgDNSServer_Click() On Error Resume Next Dim Adapter As Object 'Change DNS Severs Set Adapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & gintNetworkAdapter & "") If lblDNSServers.Tag = 0 Then lblDNSServers = Adapter.DNSServerSearchOrder(1) lblDNSServers.Tag = 1 Else lblDNSServers = Adapter.DNSServerSearchOrder(0) lblDNSServers.Tag = 0 End IfSet Adapter = Nothing End SubPrivate Sub cmdOK_Click() End End SubPrivate Sub cmdRelease_Click() On Error Resume Next Dim ReleaseLease As Object Set ReleaseLease = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & gintNetworkAdapter & "") ReleaseLease.ReleaseDHCPLease 'Clear Info Call clearall 'Disable/Enable Buttons cmdRenew.Enabled = True cmdRelease.Enabled = False
Set ReleaseLease = Nothing End Sub Sub GetAdapterData() On Error Resume Next
Dim Adapter As SWbemObject Dim NIC As SWbemObject
'Enumerate the instances
Set NICS = Namespace.InstancesOf("Win32_NetworkAdapterConfiguration") For Each NIC In NICS ' Use the RelPath property of the instance path Set NIC = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=Adapter.Path_.RelPath") 'Fill the Adapter with all the adapters cboAdapter.AddItem NIC.Description cboAdapter.ItemData(cboAdapter.NewIndex) = NIC.Index Next cboAdapter.Text = cboAdapter.List(0)
Set NICS = Nothing End SubPrivate Sub cmdRenew_Click() Me.MousePointer = vbHourglass On Error Resume Next Dim RenewLease As Object Set RenewLease = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & gintNetworkAdapter & "") 'Renew DHCP Lease for the selected adapter RenewLease.RenewDHCPLease 'Clear Info Call clearall 'Get Adapter Congiguration Call GetIPConfig(gintNetworkAdapter) 'Disable/Enable Buttons cmdRenew.Enabled = True cmdRelease.Enabled = True
Set RenewLease = Nothing Me.MousePointer = vbDefault End SubPrivate Sub Form_Load() Set Namespace = GetObject("winmgmts:") 'Get Machine Adapters GetAdapterData 'Load Default Adapter Information Call GetIPConfig(0) End SubSub GetIPConfig(intNetworkAdapter As Long) On Error Resume Next Dim Adapter As Object 'Clear Adapter Address lblAdapterAddress.Caption = "" Set Adapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & intNetworkAdapter & "") 'Fill Adapter and Host Information With Adapter lblAdapterAddress.Caption = .MACAddress If Not .IPADDRESS(intNetworkAdapter) = SystemAddress Then lblIPAddress.Caption = .IPADDRESS(intNetworkAdapter) _ Else lblIPAddress.Caption = "0.0.0.0" If Not .IPSubnet(intNetworkAdapter) = SystemAddress Then lblSubnetMask.Caption = .IPSubnet(intNetworkAdapter) _ Else lblSubnetMask.Caption = "0.0.0.0" If Not .DefaultIPGateway(intNetworkAdapter) = SystemAddress Then lblDefGateway.Caption = .DefaultIPGateway(intNetworkAdapter) _ Else lblDefGateway.Caption = "" lblDHCPServer.Caption = .DHCPServer If Not .WINSPrimaryServer = SystemAddress Then lblPrimaryWins.Caption = .WINSPrimaryServer _ Else lblPrimaryWins.Caption = "" If Not .WINSSecondaryServer = SystemAddress Then lblSecondaryWins.Caption = .WINSSecondaryServer _ Else lblSecondaryWins.Caption = "" lblLeaseObtained.Caption = ParseDate(.DHCPLeaseObtained) lblLeaseExpires.Caption = ParseDate(.DHCPLeaseExpires) lblHostName.Caption = .DNSHostName & "." & .DNSdomain lblDNSServers.Caption = .DNSServerSearchOrder(intNetworkAdapter) lblDNSServers.Tag = 0'DHCP Enabled If .DHCPEnabled = True Then chkDHCPEnabled.Value = 1 Else chkDHCPEnabled.Value = 0 End If
End With Set Adapter = Nothing End Sub Function ParseDate(sDateString As String) As StringDim sParseDate As String Dim sParseTime As StringIf sDateString = "" Then ParseDate = "" Else 'Parse Date and Time from string sParseDate = Left(sDateString, 8) sParseTime = Right(Left(sDateString, 14), 6) 'Parse to readable Date and time ParseDate = Mid(sParseDate, 5, 2) & "/" & Mid(sParseDate, 7, 2) & "/" & Mid(sParseDate, 1, 4) & " " & Mid(sParseTime, 1, 2) & ":" & Mid(sParseTime, 3, 2) & ":" & Mid(sParseTime, 5, 2) End If End Function Sub clearall() Dim Control For Each Control In Me.Controls If TypeOf Control Is Label Then If Not Control.Tag = "NOCLEAR" Then Control.Caption = "" End If End If If TypeOf Control Is CheckBox Then Control.Value = 0 Next Control
'模块部分 Option Explicit '*********************************************** '* IP Wizard * '* Copyright ?2000-2001 , Kemtech Software * '* Michael J. Kempf * '***********************************************'API to Get a Windows Version Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _ (ByRef lpVersionInformation As OSVERSIONINFO) As LongPublic Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long dwBuildNumber As Long dwPlatformId As Long szCSDVersion As String * 128 End Type' Returns Version of Windows as a String ' NOTE: Win95 returns "4.00" 'Win98 returns "4.10" 'WinNT returns "" 'Win2000 returns "5.00"Function WindowsVersion() As String Dim osInfo As OSVERSIONINFO
'窗体部分
Dim gintNetworkAdapter As LongPrivate Namespace As SWbemServices
Private Method As SWbemMethod
'Default System Address
Const SystemAddress = "127.0.0.0"
Private Sub cboAdapter_Click()
'ClearAll Controls
Call clearall
'Get Configuration for the selected adapter Call GetIPConfig(cboAdapter.ItemData(cboAdapter.ListIndex))
'Set a Global Variable for the selected adapter
gintNetworkAdapter = cboAdapter.ItemData(cboAdapter.ListIndex)
End SubPrivate Sub cmdChgDNSServer_Click()
On Error Resume Next
Dim Adapter As Object
'Change DNS Severs
Set Adapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & gintNetworkAdapter & "")
If lblDNSServers.Tag = 0 Then
lblDNSServers = Adapter.DNSServerSearchOrder(1)
lblDNSServers.Tag = 1
Else
lblDNSServers = Adapter.DNSServerSearchOrder(0)
lblDNSServers.Tag = 0
End IfSet Adapter = Nothing
End SubPrivate Sub cmdOK_Click()
End
End SubPrivate Sub cmdRelease_Click()
On Error Resume Next
Dim ReleaseLease As Object
Set ReleaseLease = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & gintNetworkAdapter & "") ReleaseLease.ReleaseDHCPLease
'Clear Info
Call clearall
'Disable/Enable Buttons
cmdRenew.Enabled = True
cmdRelease.Enabled = False
Set ReleaseLease = Nothing
End Sub
Sub GetAdapterData()
On Error Resume Next
Dim Adapter As SWbemObject
Dim NIC As SWbemObject
'Enumerate the instances
Set NICS = Namespace.InstancesOf("Win32_NetworkAdapterConfiguration") For Each NIC In NICS
' Use the RelPath property of the instance path
Set NIC = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=Adapter.Path_.RelPath")
'Fill the Adapter with all the adapters
cboAdapter.AddItem NIC.Description cboAdapter.ItemData(cboAdapter.NewIndex) = NIC.Index
Next
cboAdapter.Text = cboAdapter.List(0)
Set NICS = Nothing
End SubPrivate Sub cmdRenew_Click()
Me.MousePointer = vbHourglass
On Error Resume Next
Dim RenewLease As Object
Set RenewLease = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & gintNetworkAdapter & "")
'Renew DHCP Lease for the selected adapter
RenewLease.RenewDHCPLease
'Clear Info
Call clearall
'Get Adapter Congiguration
Call GetIPConfig(gintNetworkAdapter)
'Disable/Enable Buttons
cmdRenew.Enabled = True
cmdRelease.Enabled = True
Set RenewLease = Nothing
Me.MousePointer = vbDefault
End SubPrivate Sub Form_Load()
Set Namespace = GetObject("winmgmts:")
'Get Machine Adapters
GetAdapterData
'Load Default Adapter Information
Call GetIPConfig(0)
End SubSub GetIPConfig(intNetworkAdapter As Long)
On Error Resume Next
Dim Adapter As Object
'Clear Adapter Address
lblAdapterAddress.Caption = ""
Set Adapter = GetObject("winmgmts:Win32_NetworkAdapterConfiguration=" & intNetworkAdapter & "")
'Fill Adapter and Host Information
With Adapter
lblAdapterAddress.Caption = .MACAddress
If Not .IPADDRESS(intNetworkAdapter) = SystemAddress Then lblIPAddress.Caption = .IPADDRESS(intNetworkAdapter) _
Else lblIPAddress.Caption = "0.0.0.0"
If Not .IPSubnet(intNetworkAdapter) = SystemAddress Then lblSubnetMask.Caption = .IPSubnet(intNetworkAdapter) _
Else lblSubnetMask.Caption = "0.0.0.0"
If Not .DefaultIPGateway(intNetworkAdapter) = SystemAddress Then lblDefGateway.Caption = .DefaultIPGateway(intNetworkAdapter) _
Else lblDefGateway.Caption = ""
lblDHCPServer.Caption = .DHCPServer
If Not .WINSPrimaryServer = SystemAddress Then lblPrimaryWins.Caption = .WINSPrimaryServer _
Else lblPrimaryWins.Caption = ""
If Not .WINSSecondaryServer = SystemAddress Then lblSecondaryWins.Caption = .WINSSecondaryServer _
Else lblSecondaryWins.Caption = ""
lblLeaseObtained.Caption = ParseDate(.DHCPLeaseObtained)
lblLeaseExpires.Caption = ParseDate(.DHCPLeaseExpires)
lblHostName.Caption = .DNSHostName & "." & .DNSdomain
lblDNSServers.Caption = .DNSServerSearchOrder(intNetworkAdapter)
lblDNSServers.Tag = 0'DHCP Enabled
If .DHCPEnabled = True Then
chkDHCPEnabled.Value = 1
Else
chkDHCPEnabled.Value = 0
End If
End With
Set Adapter = Nothing
End Sub
Function ParseDate(sDateString As String) As StringDim sParseDate As String
Dim sParseTime As StringIf sDateString = "" Then
ParseDate = ""
Else
'Parse Date and Time from string
sParseDate = Left(sDateString, 8)
sParseTime = Right(Left(sDateString, 14), 6)
'Parse to readable Date and time
ParseDate = Mid(sParseDate, 5, 2) & "/" & Mid(sParseDate, 7, 2) & "/" & Mid(sParseDate, 1, 4) & " " & Mid(sParseTime, 1, 2) & ":" & Mid(sParseTime, 3, 2) & ":" & Mid(sParseTime, 5, 2)
End If
End Function
Sub clearall()
Dim Control For Each Control In Me.Controls
If TypeOf Control Is Label Then
If Not Control.Tag = "NOCLEAR" Then
Control.Caption = ""
End If
End If
If TypeOf Control Is CheckBox Then Control.Value = 0
Next Control
lblIPAddress = "0.0.0.0"
lblSubnetMask = "0.0.0.0"
End Sub
'模块部分
Option Explicit
'***********************************************
'* IP Wizard *
'* Copyright ?2000-2001 , Kemtech Software *
'* Michael J. Kempf *
'***********************************************'API to Get a Windows Version
Public Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
(ByRef lpVersionInformation As OSVERSIONINFO) As LongPublic Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type' Returns Version of Windows as a String
' NOTE: Win95 returns "4.00"
'Win98 returns "4.10"
'WinNT returns ""
'Win2000 returns "5.00"Function WindowsVersion() As String
Dim osInfo As OSVERSIONINFO
osInfo.dwOSVersionInfoSize = Len(osInfo)
GetVersionEx osInfo
WindowsVersion = osInfo.dwMajorVersion & "." & Right$("0" & Format$ _
(osInfo.dwMinorVersion), 2)
End Function
RAS 同步适配器
等等
即GetAdapterData的值
出现自动化错误,应该怎么解决?