Detecting LAN Computer Names

jdemackio

Active member
Joined
Jun 8, 2004
Messages
42
I would like to populate a listbox with the windows networking name (computer name) of all clients within a LAN. Can anyone point me in the right direction?
 
Might want to look at the API function NetServerEnum, which you can use to find all pc names for a given domain name
 
jdemackio said:
Ok, I will look into that, thank you for your quick response.

No problem. If you want some sample code, try downloading the SOSOS application from planet source code. Its probably in the Hall of Fame section (or something like that).
 
Here is a sample class that lists SQL Servers on the Network, you should be able to moderfy it for all machines.

Code:
Public Class clsNetAPI32
    Private Const SV_TYPE_SQLSERVER = &H4S
     NetServerEnum Return Values 
    Private Const ERR_SUCCESS = 0
    Private Const ERROR_MORE_DATA As Int32 = 234
    Private Const ERROR_NO_BROWSER_SERVERS_FOUND As Int32 = 6118I

    Private Structure SERVER_INFO_100
        Dim sv100_platform_id As Integer
        Dim sv100_name As Integer
    End Structure
     
     level : Specifies the information level of the data 
     bufptr : Pointer to the buffer that receives the data 
     prefmaxlen : Specifies the preferred maximum length of returned data, in bytes 
     entriesread : Pointer to a value that receives the count of elements actually enumerated 
     totalentries : Pointer to a value that receives the total number of visible servers and workstations on the network 
     servertype : Specifies a value that filters the server entries to return from the enumeration 
     domain : Pointer to a constant string that specifies the name of the domain for which a list of servers is to be returned 
     resume_handle : Reserved; must be set to zero 
    Private Declare Unicode Function NetServerEnum Lib "netapi32" _
          (ByVal servername As IntPtr _
          , ByVal level As Integer _
          , ByRef bufptr As IntPtr _
          , ByVal prefmaxlen As Integer _
          , ByRef entriesread As Integer _
          , ByRef totalentries As Integer _
          , ByVal servertype As Integer _
          , ByVal domain As IntPtr _
          , ByVal resume_handle As Integer _
          ) As Integer

    Private Declare Function NetApiBufferFree Lib "netapi32" _
     (ByVal BufPtr As IntPtr) As Integer

    Public Function GetListOfServers(ByRef colSQLServers As Collection) As Boolean

        Dim sServerName As IntPtr = IntPtr.Zero
        Dim iLevel As Integer = 100
        Dim lBufPtr As IntPtr
        Dim iprefMaxLen As Integer = -1
        Dim iEntriesRead As Integer
        Dim iTotalEntries As Integer
        Dim sDomain As IntPtr = IntPtr.Zero
        Dim iResumeHandle As Integer = 0
        Dim iReturnValue As Integer

        Dim sctSrvList As SERVER_INFO_100
        Dim nStructSize As Integer = Marshal.SizeOf(sctSrvList)
        Dim scSQLServers As New Specialized.StringCollection
        Dim iIndex As Integer

        colSQLServers = New Collection
        iReturnValue = NetServerEnum(sServerName _
                                    , iLevel _
                                    , lBufPtr _
                                    , iprefMaxLen _
                                    , iEntriesRead _
                                    , iTotalEntries _
                                    , SV_TYPE_SQLSERVER _
                                    , sDomain _
                                    , iResumeHandle _
                                    )
        If iReturnValue = ERR_SUCCESS Then
             Loop through each SQL Server returned by the NetServerEnum call 
            For iIndex = 0 To iEntriesRead - 1
                 Convert each SQL Server entry into a SERVER_INFO_100 structure 
                sctSrvList = DirectCast(Marshal.PtrToStructure(New IntPtr(lBufPtr.ToInt32 + (nStructSize * iIndex)) _
                                                              , GetType(SERVER_INFO_100) _
                                                              ) _
                                       , SERVER_INFO_100 _
                                       )
                 Add each SQL Server SERVER_INFO_100 structure entry into the StringCollection 
                scSQLServers.Add(Marshal.PtrToStringUni(New IntPtr(sctSrvList.sv100_name)))
            Next

        End If

         Clean up regardless of iReturnValue 
        Call NetApiBufferFree(lBufPtr)

         Move from String Collection to Array To Collection 
         I only return the list as a collection because 
         collections are what I am used to 
        Dim sSQLServers(scSQLServers.Count - 1) As String
        scSQLServers.CopyTo(sSQLServers, 0)
        For iIndex = 0 To sSQLServers.GetUpperBound(0)
            colSQLServers.Add(sSQLServers(iIndex))
        Next
        Return True

    End Function

End Class
 
One question, I try to see that example, but the word "Marshal" say that its not declared.
First, what is marshall.. and how can I declare it?
 
At the top of the source file you will need to add
Code:
Imports System.Runtime.InteropServices
 
Last edited by a moderator:
Back
Top