How to enumerate all machines on a network

Posted on 2005-05-02
Medium Priority
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
  • 2
  • 2
LVL 96

Accepted Solution

Bob Learned earned 2000 total points
ID: 13908908
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
ID: 13957922
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
ID: 13959256

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
ID: 13961726
Hi Bob

Thank u

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

840 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