Link to home
Start Free TrialLog in
Avatar of nordicaccess
nordicaccessFlag for Norway

asked on

Display mac and ip in listview

Hello

I hope somebody can help me, I have a code that display remote computers on a wireless network. The address are displayed in a listview, but the problem is that only 2 dynamic address are showed not the third or  the fourth address.

I have 3 computers and a wireless router that I test against
192.168.0.1
192.168.0.102
192.168.0.104
192.168.0.108

In my listview only 192.168.0.1 and 192.168.0.102 will bee displayed

If I turn off 192.168.0.102 then the list will display
192.168.0.1 and 192.168.0.104


Hope somebody can help me

VB net 2008

Apologise my bad English

___________________________________________________
Button on my form
Dim iptable As New GetIpNetTable
        Dim array As ArrayList
        arplist.View = View.Details
        arplist.Columns.Add("Ip Address", 100, HorizontalAlignment.Left)
        arplist.Columns.Add("MAC Address", 100, HorizontalAlignment.Left)
        array = iptable.LoadTableEntries
        For i As Integer = 0 To array.Count - 1
            arplist.Items.Add(array.Item(i).Value).SubItems.Add(array.Item(i).Key)
        Next
_____________________________________________________________
 
Modul 
Imports System.ComponentModel
Imports System.Text
Imports System
Imports System.Reflection
Imports System.Resources
Imports System.Runtime.InteropServices
Module arp
    Public Class GetIpNetTable
        Private Const MAXLEN_PHYSADDR As Integer = 6
 
        Private Declare Function GetIpNetTable Lib "Iphlpapi" ( _
        ByVal pIpNetTable As IntPtr, _
        ByRef pdwSize As Integer, ByVal bOrder As Boolean) As Integer
        Private Structure MIB_IPNETROW
            Dim dwIndex As Integer
            Dim dwPhysAddrLen As Integer
            <MarshalAs(UnmanagedType.ByValArray, SizeConst:=MAXLEN_PHYSADDR)> Dim dwPhysAddr As Byte()
            Dim dwAddr As Integer
            Dim dwStructure As Integer
        End Structure 'MIB_IPNETROW' 
 
        Private Function ConvertMacAddress(ByVal byteArray() As Byte) As String
            Dim builder As New StringBuilder
            For Each byteCurrent As Byte In byteArray
                builder.Append(byteCurrent.ToString("X").PadLeft(2, "0") & "-")
            Next byteCurrent
            Return builder.Remove(builder.Length - 1, 1).ToString
        End Function 'ConvertMacAddress' 
 
        Private Function IntegerToByteArray(ByVal number As Integer) As Byte()
            ' Destination byte array. 
            Dim byteArray(3) As Byte
            ' Create Gchandle instance and pin variable required, so the garbage collector won't move it. 
            Dim handle As GCHandle = GCHandle.Alloc(number, GCHandleType.Pinned)
            ' Get address of variable in pointer variable. 
            Dim address As IntPtr = handle.AddrOfPinnedObject()
            ' Use copy method to copy number to byte array. 
            Marshal.Copy(address, byteArray, 0, 4)
            Return byteArray
        End Function 'IntegerToByteArray' 
 
        Private Function ConvertIpAddress(ByVal byteArray() As Byte) As String
            Dim address As String = String.Empty
            For Each byteCurrent As Byte In byteArray
                address &= byteCurrent.ToString & "."
            Next byteCurrent
            Return address.Remove(address.Length - 1, 1)
        End Function 'ConvertIpAddress' 
 
        Public Function LoadTableEntries() As ArrayList
            Const ERROR_INSUFFICIENT_BUFFER As Integer = &H7A
            Dim listEntries As New ArrayList
            ' The number of bytes needed. 
            Dim bytesNeeded As Integer = 0
            ' The result from the API call. 
            Dim result As Integer = GetIpNetTable(IntPtr.Zero, bytesNeeded, False)
            ' Call the function, expecting an insufficient buffer. 
            If result <> ERROR_INSUFFICIENT_BUFFER Then
                Throw New Win32Exception(result)
            End If
            ' Allocate the memory, do it in a try/finally block, to ensure 
            ' that it is released. 
            Dim Buffer As IntPtr = IntPtr.Zero
            Try
                ' Allocate the memory. 
                Buffer = Marshal.AllocCoTaskMem(bytesNeeded)
                ' Make the call again. If it did not succeed, then 
                ' raise an error. 
                result = GetIpNetTable(Buffer, bytesNeeded, False)
                ' If the result is not 0 (no error), then throw an exception. 
                If (result <> 0) Then
                    ' Throw an exception. 
                    Throw New Win32Exception(result)
                Else
                    ' Now we have the buffer, we have to marshal it. We can read() 
                    ' the first 4 bytes to get the length of the buffer. 
                    Dim entries As Integer = Marshal.ReadInt32(Buffer)
                    ' Increment the memory pointer by the size of the int. 
                    Dim currentBuffer As New IntPtr(Buffer.ToInt64() + _
                    Marshal.SizeOf(GetType(Integer)))
                    ' Allocate an array of entries. 
                    Dim table(entries) As MIB_IPNETROW
                    ' Cycle through the entries. 
                    For index As Integer = 0 To entries - 1
 
                        table(index) = Marshal.PtrToStructure(currentBuffer, GetType(MIB_IPNETROW))
                        Dim macAddress As String = ConvertMacAddress(table(index).dwPhysAddr)
                        Dim ipAddress As String = ConvertIpAddress(IntegerToByteArray(table(index).dwAddr))
                        listEntries.Add(New DictionaryEntry(macAddress, ipAddress))
                        currentBuffer = New IntPtr(Buffer.ToInt64 + 4 + Marshal.SizeOf(GetType(MIB_IPNETROW)))
                        'entries += 4
                    Next
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                ' Release the memory. 
                Marshal.FreeCoTaskMem(Buffer)
            End Try
            Return listEntries
        End Function 'LoadTableEntries' 
 
    End Class
 
End Module

Open in new window

listview.png
arp-dos.png
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

try this accepted answer in this thread...
ASKER CERTIFIED SOLUTION
Avatar of JackOfPH
JackOfPH
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nordicaccess

ASKER

Thank you for your answer, I have found a link that I think will  solved my problem.
It's in German
 
http://www.tech-archive.net/Archive/German/Entwicklung/microsoft.public.de.german.entwickler.dotnet.vb/2005-12/msg01011.html
 
 
Thank you for your replay