需要创建名称为 TeMac 和 TeIp 的文本框,用来接收信息。
Private Sub Command1_Click()
Dim Mac
strComputer = "."
Mac = ""
'获得MAC信息
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!//" & strComputer & "/root/cimv2") '''加上这个
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each objItem In colItems
Mac = Mac + " " + objItem.macaddress
Next
TeMac.Text = Replace(Trim(Mac), ":", "-")
strComputer = "."
Set objWMI = GetObject("winmgmts://" & strComputer & "/root/cimv2")
Set colIP = objWMI.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each Ip In colIP
If Not IsNull(Ip.ipaddress) Then
For i = LBound(Ip.ipaddress) To UBound(Ip.ipaddress)
'Mac = Ip.macaddress(I)objObject.DNSHostName
If Ip.ipaddress(i) = "0.0.0.0" Then
Else
TeIp.Text = Trim(TeIp.Text + " " + Ip.ipaddress(i))
End If
'MsgBox "IP 地址:" & IP.ipaddress(I) & Chr(10) & _
"网卡类型:" & IP.Description(I) & Chr(10) & _
"网卡地址:" & IP.macaddress(I)
Next
End If
Next
End Sub