Click here to Skip to main content
15,885,891 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 .
Posted
Updated 21-Feb-13 10:30am
v2
Comments
Vedangi 13-Jun-12 4:04am    
You can go through this link .
http://www.devasp.net/net/articles/display/684.html

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[^]
VB
''' <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>
Const ERROR_INSUFFICIENT_BUFFER As Integer = 122
''' <summary>
''' MIB_IPNETROW structure returned by GetIpNetTable
''' DO NOT MODIFY THIS STRUCTURE.
''' </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
    Try
        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)
        Next
        ' 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
        Next
    Finally
        ' Release the memory.
        Marshal.FreeCoTaskMem(rawTable)
    End Try
    Return all
End Function
 
Share this answer
 
Comments
Jαved 13-Jun-12 5:09am    
Perfect +5.
try this it will give all connected machine Name over the LAN....

C#
Imports System.IO
Imports System.DirectoryServices
Public Class Form1

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

    Sub FindingThreats()
        ListView1.Items.Clear()
        Dim childEntry As DirectoryEntry
        Dim ParentEntry As New DirectoryEntry
        Try
            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"
                                    ListView1.Items.Add(SubChildEntry.Name)

                            End Select
                        Next
                End Select
            Next
        Catch Excep As Exception
            MsgBox("Error While Reading Directories : " + Excep.Message.ToString)
        Finally
            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

Regards

Sarva
 
Share this answer
 
v2
Comments
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..
regards
sarva
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.

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

your code fetches names of computer from the windows network directory...it 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!

regards
nikhil
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