How to enumerate all machines on a network

Hi Experts
How do we enumerate all machines on a network.
i found a nice work in VB here

i need a similar replacement in VB.Net
Thanks in advance

LVL 14
Shiju SasidharanAssoc Project ManagerAsked:
Who is Participating?
Bob LearnedConnect With a Mentor Commented:
Imports System.Runtime.InteropServices

Public Class EnumerateMachines

  Public Class ServerInfo

    Public Name As String
    Public PlatformId As Int32
    Public VersionMajor As Integer
    Public VersionMinor As Integer
    Public Type As ServerType
    Public Comment As String

  End Class

  Private Structure SERVER_INFO_101

    Dim PlatformID As Integer
    Dim Name As IntPtr     'Pointer to string
    Dim VersionMajor As Integer
    Dim VersionMinor As Integer
    Dim Type As Integer
    Dim Comment As IntPtr   'Pointer to string

  End Structure

  Private Declare Auto Function NetServerEnum Lib "netapi32.dll" _
    (ByVal nullptr As Integer, ByVal Level As Integer, ByRef BufPtr As IntPtr, _
     ByVal BufMaxLen As Integer, ByRef EntriesRead As Integer, _
     ByRef TotalEntries As Integer, ByVal ServerType As Integer, _
     ByVal Domain As IntPtr, ByVal ResumeHandle As Integer) As Integer

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

  Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
    (ByRef pTo As Object, ByRef uFrom As Object, ByVal lSize As Integer)

  Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Integer) As Integer

  Private Const MAX_PREFERRED_LENGTH As Integer = -1
  Private Const NERR_SUCCESS As Integer = 0
  Private Const ERROR_MORE_DATA As Integer = 234

  Public Enum ServerType
    Workstation = &H1S
    Server = &H2S
    SQLServer = &H4S
    DomainController = &H8S
    DomainBackupController = &H10S
    TimeSource = &H20S
    AFP = &H40S
    Novell = &H80S
    DomainMember = &H100S
    PrintQServer = &H200S
    DialInServer = &H400S
    XenixServer = &H800S
    UnixServer = XenixServer
    NT = &H1000S
    WFW = &H2000S
    MFPNServer = &H4000S
    NTServer = &H8000S
    PotentialBrowser = &H10000
    BackupBrowser = &H20000
    MasterBrowser = &H40000
    DomainMaster = &H80000
    OSFServer = &H100000
    VMSServer = &H200000
    Windows = &H400000 'Windows95 +
    DFS = &H800000 'Root of a DFS tree
    NTCluster = &H1000000 'NT Cluster
    TerminalServer = &H2000000 'Terminal Server
    DCE = &H10000000 'IBM DSS
    XPortAlternate = &H20000000 'return alternate transport
    LocalListOnly = &H40000000 'return local only
    Domain = &H80000000
    All = &HFFFFFFFF
  End Enum

  Private Const SV_PLATFORM_ID_OS2 As Integer = 400
  Private Const SV_PLATFORM_ID_NT As Integer = 500

  Private Const PLATFORM_ID_DOS As Integer = 300
  Private Const PLATFORM_ID_OS2 As Integer = 400
  Private Const PLATFORM_ID_NT As Integer = 500
  Private Const PLATFORM_ID_OSF As Integer = 600
  Private Const PLATFORM_ID_VMS As Integer = 700

  'Mask applied to svX_version_major in
  'order to obtain the major version number
  Private Const MAJOR_VERSION_MASK As Integer = &HFS

  Public Function GetServers(ByVal type As ServerType) As ArrayList

    Dim alstServers As New ArrayList

    'lists all servers of the specified type
    'that are visible in a domain.
    Dim lptrBuffer, iPtrNext As IntPtr
    Dim dwTotalentries As Integer
    Dim dwEntriesRead As Integer
    Dim dwResumeHandle As Integer
    Dim structServerInfo As SERVER_INFO_101
    Dim success As Integer
    Dim cnt As Integer
    Dim intIterator As Integer

    Dim intSize As Integer = Marshal.SizeOf(structServerInfo)

    success = NetServerEnum(0, 101, lptrBuffer, MAX_PREFERRED_LENGTH, dwEntriesRead, dwTotalentries, type, IntPtr.Zero, dwResumeHandle)

    'if all goes well
    If success = NERR_SUCCESS And success <> ERROR_MORE_DATA Then

      'loop through the returned data, adding each
      'machine to the list
      For cnt = 0 To dwEntriesRead - 1

        'Get Pointer to next structure
        iPtrNext = New IntPtr(lptrBuffer.ToInt32 + (intSize * intIterator))

        'Convert structure pointer to structure
        structServerInfo = Marshal.PtrToStructure(iPtrNext, GetType(SERVER_INFO_101))

        Dim objServer As New ServerInfo

        objServer.Name = Marshal.PtrToStringUni(structServerInfo.Name)
        objServer.Comment = Marshal.PtrToStringUni(structServerInfo.Comment)
        objServer.PlatformId = structServerInfo.PlatformID
        objServer.Type = structServerInfo.Type
        objServer.VersionMajor = structServerInfo.VersionMajor
        objServer.VersionMinor = structServerInfo.VersionMinor



    End If

    ' Clean up resources.
    Call NetApiBufferFree(lptrBuffer)

    Return alstServers

  End Function 'GetServers'

End Class

Shiju SasidharanAssoc Project ManagerAuthor Commented:
Hi Bob
i tried ur code , thanks
it displays repeated values , i think moving next pointer has some problem

>>iPtrNext = New IntPtr(lptrBuffer.ToInt32 + (intSize * intIterator))

 i made this change. its working now.

iPtrNext = New IntPtr(lptrBuffer.ToInt32 + (intSize * cnt))

please make ur comment ...

Bob LearnedCommented:

This was code converted from VB6, and relatively untested, and if it works as changed, then go with it.  I will make a note in my code to check this on a later date, since I don't use this code now.

Shiju SasidharanAssoc Project ManagerAuthor Commented:
Hi Bob

Thank u
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.