|
在用无线网卡后,电脑就有2个MAC地址,我想只获取主板本地的MAC地址,百度上用winmgmts 的方式都是优先获取无线网卡的。我在VBNET上获取了代码,但不会转换为VBA的 求各位大神帮忙解答下 谢谢,或提供好的实例获取主板本地MAC地址 以下是VBNET 获取主机本地MAC 的代码:
Public Function GetMacByNetworkInterface() As String
Dim key As String = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\"
Dim macAddress As String = String.Empty
Try
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
For Each adapter As NetworkInterface In nics
If adapter.NetworkInterfaceType = NetworkInterfaceType.Ethernet AndAlso adapter.GetPhysicalAddress().ToString().Length <> 0 Then
Dim fRegistryKey As String = key + adapter.Id & Convert.ToString("\Connection")
Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey(fRegistryKey, False)
If rk IsNot Nothing Then
Dim fPnpInstanceID As String = rk.GetValue("PnpInstanceID", "").ToString()
Dim fMediaSubType As Integer = Convert.ToInt32(rk.GetValue("MediaSubType", 0))
If fPnpInstanceID.Length > 3 AndAlso fPnpInstanceID.Substring(0, 3) = "PCI" Then
macAddress = adapter.GetPhysicalAddress().ToString()
For i As Integer = 1 To 5
macAddress = macAddress.Insert(3 * i - 1, ":")
Next
Exit For
End If
End If
End If
Next
'这里写异常的处理(最好写入日志文件)
Catch ex As Exception
End Try
Return macAddress
End Function
|
|