How to enumerate all machines on a network

Posted on 2005-05-02
Last Modified: 2010-08-05
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

Question by:Shiju Sasidharan
    LVL 96

    Accepted Solution

    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

    LVL 14

    Author Comment

    by:Shiju Sasidharan
    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 ...

    LVL 96

    Expert Comment

    by:Bob Learned

    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.

    LVL 14

    Author Comment

    by:Shiju Sasidharan
    Hi Bob

    Thank u

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now