Click here to Skip to main content
13,764,731 members
Rate this:
Please Sign up or sign in to vote.
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 .
Posted 12-Jun-12 22:38pm
(unknown member)
Updated 21-Feb-13 11:30am
Vedangi 13-Jun-12 4:04am
You can go through this link .
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
Jαved 13-Jun-12 5:09am
Perfect +5.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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


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
Member 12235471 8-Mar-16 6:51am
you can add it from project>>add refrence
Member 11555055 1-Apr-15 13:28pm
i am getting same erroe............TYPE directoryentry is not define
Member 12235471 8-Mar-16 6:50am
thank you it works , but how can i get ip addreses
Member 13359631 22-Sep-17 16:36pm
Wait so how i can change it for giving me the IP and not the Name?
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05-2016 | 2.8.181113.4 | Last Updated 20 Sep 2013
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100