This is untried - it is an automatic conversion of my C# method to VB. It's based on my Tip here:
Retrieving IP and MAC addresses for a LAN[
^]
<DllImport("IpHlpApi.dll")> _
Private Shared Function GetIpNetTable(pIpNetTable As IntPtr, <MarshalAs(UnmanagedType.U4)> ByRef pdwSize As Integer, bOrder As Boolean) As <MarshalAs(UnmanagedType.U4)> Integer
End Function
Const ERROR_INSUFFICIENT_BUFFER As Integer = 122
<StructLayout(LayoutKind.Sequential)> _
Private Structure MIB_IPNETROW
<MarshalAs(UnmanagedType.U4)> _
Public dwIndex As Integer
<MarshalAs(UnmanagedType.U4)> _
Public dwPhysAddrLen As Integer
<MarshalAs(UnmanagedType.U1)> _
Public mac0 As Byte
<MarshalAs(UnmanagedType.U1)> _
Public mac1 As Byte
<MarshalAs(UnmanagedType.U1)> _
Public mac2 As Byte
<MarshalAs(UnmanagedType.U1)> _
Public mac3 As Byte
<MarshalAs(UnmanagedType.U1)> _
Public mac4 As Byte
<MarshalAs(UnmanagedType.U1)> _
Public mac5 As Byte
<MarshalAs(UnmanagedType.U1)> _
Public mac6 As Byte
<MarshalAs(UnmanagedType.U1)> _
Public mac7 As Byte
<MarshalAs(UnmanagedType.U4)> _
Public dwAddr As Integer
<MarshalAs(UnmanagedType.U4)> _
Public dwType As Integer
End Structure
Public Shared Function GetAllDevicesOnLAN() As Dictionary(Of IPAddress, PhysicalAddress)
Dim all As New Dictionary(Of IPAddress, PhysicalAddress)()
all.Add(GetIPAddress(), GetMacAddress())
Dim spaceForNetTable As Integer = 0
GetIpNetTable(IntPtr.Zero, spaceForNetTable, False)
Dim rawTable As IntPtr = IntPtr.Zero
Try
rawTable = Marshal.AllocCoTaskMem(spaceForNetTable)
Dim errorCode As Integer = GetIpNetTable(rawTable, spaceForNetTable, False)
If errorCode <> 0 Then
Throw New Exception(String.Format("Unable to retrieve network table. Error code {0}", errorCode))
End If
Dim rowsCount As Integer = Marshal.ReadInt32(rawTable)
Dim currentBuffer As New IntPtr(rawTable.ToInt64() + Marshal.SizeOf(GetType(Int32)))
Dim rows As MIB_IPNETROW() = New MIB_IPNETROW(rowsCount - 1) {}
For index As Integer = 0 To rowsCount - 1
rows(index) = CType(Marshal.PtrToStructure(New IntPtr(currentBuffer.ToInt64() + (index * Marshal.SizeOf(GetType(MIB_IPNETROW)))), GetType(MIB_IPNETROW)), MIB_IPNETROW)
Next
Dim virtualMAC As New PhysicalAddress(New Byte() {0, 0, 0, 0, 0, 0})
Dim broadcastMAC As New PhysicalAddress(New Byte() {255, 255, 255, 255, 255, 255})
For Each row As MIB_IPNETROW In rows
Dim ip As New IPAddress(BitConverter.GetBytes(row.dwAddr))
Dim rawMAC As Byte() = New Byte() {row.mac0, row.mac1, row.mac2, row.mac3, row.mac4, row.mac5}
Dim pa As New PhysicalAddress(rawMAC)
If Not pa.Equals(virtualMAC) AndAlso Not pa.Equals(broadcastMAC) AndAlso Not IsMulticast(ip) Then
If Not all.ContainsKey(ip) Then
all.Add(ip, pa)
End If
End If
Next
Finally
Marshal.FreeCoTaskMem(rawTable)
End Try
Return all
End Function