Click here to Skip to main content
15,666,844 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
need to get a code to show all pc's connected to network and which will be shown in the list to select for further action .
Updated 21-Feb-13 10:30am
Vedangi 13-Jun-12 4:04am    
You can go through this link .

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[^]
''' <summary>
''' GetIpNetTable external method
''' </summary>
''' <param name="pIpNetTable"></param>
''' <param name="pdwSize"></param>
''' <param name="bOrder"></param>
''' <returns></returns>
<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

''' <summary>
''' Error codes GetIpNetTable returns that we recognise
''' </summary>
''' <summary>
''' MIB_IPNETROW structure returned by GetIpNetTable
''' </summary>
<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

''' <summary>
''' Get the IP and MAC addresses of all known devices on the LAN
''' </summary>
''' <remarks>
''' 1) This table is not updated often - it can take some human-scale time
'''    to notice that a device has dropped off the network, or a new device
'''    has connected.
''' 2) This discards non-local devices if they are found - these are multicast
'''    and can be discarded by IP address range.
''' </remarks>
''' <returns></returns>
Public Shared Function GetAllDevicesOnLAN() As Dictionary(Of IPAddress, PhysicalAddress)
    Dim all As New Dictionary(Of IPAddress, PhysicalAddress)()
    ' Add this PC to the list...
    all.Add(GetIPAddress(), GetMacAddress())
    Dim spaceForNetTable As Integer = 0
    ' Get the space needed
    ' We do that by requesting the table, but not giving any space at all.
    ' The return value will tell us how much we actually need.
    GetIpNetTable(IntPtr.Zero, spaceForNetTable, False)
    ' Allocate the space
    ' We use a try-finally block to ensure release.
    Dim rawTable As IntPtr = IntPtr.Zero
        rawTable = Marshal.AllocCoTaskMem(spaceForNetTable)
        ' Get the actual data
        Dim errorCode As Integer = GetIpNetTable(rawTable, spaceForNetTable, False)
        If errorCode <> 0 Then
            ' Failed for some reason - can do no more here.
            Throw New Exception(String.Format("Unable to retrieve network table. Error code {0}", errorCode))
        End If
        ' Get the rows count
        Dim rowsCount As Integer = Marshal.ReadInt32(rawTable)
        Dim currentBuffer As New IntPtr(rawTable.ToInt64() + Marshal.SizeOf(GetType(Int32)))
        ' Convert the raw table to individual entries
        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)
        ' Define the dummy entries list (we can discard these)
        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
                'Console.WriteLine("IP: {0}\t\tMAC: {1}", ip.ToString(), pa.ToString());
                If Not all.ContainsKey(ip) Then
                    all.Add(ip, pa)
                End If
            End If
        ' Release the memory.
    End Try
    Return all
End Function
Share this answer
Jαved 13-Jun-12 5:09am    
Perfect +5.
try this it will give all connected machine Name over the LAN....

Imports System.IO
Imports System.DirectoryServices
Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    End Sub

    Sub FindingThreats()
        Dim childEntry As DirectoryEntry
        Dim ParentEntry As New DirectoryEntry
            ParentEntry.Path = "WinNT:"
            For Each childEntry In ParentEntry.Children
                Select Case childEntry.SchemaClassName
                    Case "Domain"

                        Dim SubChildEntry As DirectoryEntry
                        Dim SubParentEntry As New DirectoryEntry
                        SubParentEntry.Path = "WinNT://" & childEntry.Name
                        For Each SubChildEntry In SubParentEntry.Children
                            Select Case SubChildEntry.SchemaClassName
                                Case "Computer"

                            End Select
                End Select
        Catch Excep As Exception
            MsgBox("Error While Reading Directories : " + Excep.Message.ToString)
            ParentEntry = Nothing
        End Try
    End Sub

End Class

drag and drop a list view from toolbox then set properties to

1. View - Details
2. FullRowSelect - true
3. GridLines - true

thats all


Share this answer
ShantaShan 18-Feb-13 8:01am    
can i get the full coding....TQ
Sumit Bhargav 5-Dec-13 6:53am    
Thanks Sarrrva,
That code worked perfectly for me.You save my day.
Thank you again.
Sarrrva 21-Feb-13 2:29am    
Hello shan if your requirement is get connected machine Name over the LAN....

this piece of code is enough..
Sarrrva 12-Dec-13 15:39pm    
welcome :-)
dipankarnalui 17-Mar-15 14:09pm    
Type DirectoryEntry is not defined... One error i'm getting
Use Dns.GetHostEntry Method[^] to get an IP Address for a computer (host) name.

Dim strHostName As String = ""
Dim strIPAddress As String = ""
Dim objAddressList() As System.Net.IPAddress = _
For x = 0 To objAddressList.GetUpperBound(0)
    If objAddressList(x).AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
        strIPAddress = objAddressList(x).ToString
        Exit For
     End If
Share this answer
Member 10287059 20-Sep-13 13:16pm    
Sarva(Solution 2 uploader)

your code fetches names of computer from the windows network works perfectly,
but if 2 computers on same network have same names(also are active concurrently) it shows the name of only one...actually windows directory shows either of one...we cant see the name of 2nd computer till the 1st one gets inactive.
How can we solve this problem??
please help!

Mike Meinz 20-Sep-13 16:31pm    
Computers on a network should have unique names. Computers with the same name will cause problems.

See "Duplicate Name Exists" Error Message and Duplicate Computer on Network.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900