Why list the servers with code "NetServerEnum" works perfect on one machine but not the other?

Posted on 2004-10-20
Medium Priority
Last Modified: 2008-02-01
Here my code I copied from one web site which works perfect on my machine but when run on other machines it only picked up some servers but not all?

I have tried:     Public Const SV_TYPE_SERVER = &H2
                       Public Const SV_TYPE_ALL                 As Long = &HFFFFFFFF

Please give answer with code. Thank you.

Function GetServerInfo()
        Dim lReturnCode As Long
        Dim bBuffer(512) As Byte
        Dim i As Integer, x As Integer
        Dim tSeverInfo101 As SERVER_INFO_101, lSeverInfo101 As Long
        Dim sComputerName As String
        Dim lPreferedMaxLen As Long
        Dim lEntriesRead As Long
        Dim lTotalEntries As Long
        Dim lServerType As Long
        Dim sDomain As String
        Dim vResume As Variant
        Dim lSeverInfo101StructPtr As Long
         'Clear all of the sComputerName
         sComputerName = ""
         lServerType = SV_TYPE_SERVER
         'Call NetServerEnum to get a list of Servers
         lReturnCode = NetServerEnum(ByVal 0&, 101, lSeverInfo101, lPreferedMaxLen, lEntriesRead, lTotalEntries, ByVal lServerType, ByVal sDomain, vResume)
         ' NetServerEnum Index is 1 based
         x = 1
         lSeverInfo101StructPtr = lSeverInfo101
         Do While x <= lTotalEntries
            RtlMoveMemory tSeverInfo101, ByVal lSeverInfo101StructPtr, Len(tSeverInfo101)
            lstrcpyW bBuffer(0), tSeverInfo101.wki101_servername
            'Get every other byte from Unicode string.
            i = 0
            Do While bBuffer(i) <> 0
                sComputerName = sComputerName & Chr(bBuffer(i))
                i = i + 2
            Combo_SQLServer.AddItem sComputerName
            'Debug.Print "Found server: " & sComputerName
            sComputerName = ""
            x = x + 1
            lSeverInfo101StructPtr = lSeverInfo101StructPtr + Len(tSeverInfo101)
         lReturnCode = NetApiBufferFree(lSeverInfo101)
End Function
Question by:limct

Expert Comment

ID: 12366780
Did you run this on seperate machines with the same OS? The code looks ok. maybe machines configurations affects the return value. You may check what protocols are available in ur machine and compair it with the other. Or try to check if the machine has an access to the server. I cant really see from here where the problem is. But i hope i helped. :)
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12367431
Option Explicit
'Windows type used to call the Net API
' Some of the folowing declarations are not completely needed for ur case
Private Const ERROR_SUCCESS         As Long = 0
Private Const MAX_DOMAIN_NAME_LEN   As Long = 128
Private Const MAX_HOSTNAME_LEN      As Long = 128
Private Const MAX_SCOPE_ID_LEN      As Long = 256

Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&

Private Const SV_TYPE_WORKSTATION         As Long = &H1
Private Const SV_TYPE_SERVER              As Long = &H2
Private Const SV_TYPE_SQLSERVER           As Long = &H4
Private Const SV_TYPE_DOMAIN_CTRL         As Long = &H8
Private Const SV_TYPE_DOMAIN_BAKCTRL      As Long = &H10
Private Const SV_TYPE_TIME_SOURCE         As Long = &H20
Private Const SV_TYPE_AFP                 As Long = &H40
Private Const SV_TYPE_NOVELL              As Long = &H80
Private Const SV_TYPE_DOMAIN_MEMBER       As Long = &H100
Private Const SV_TYPE_PRINTQ_SERVER       As Long = &H200
Private Const SV_TYPE_DIALIN_SERVER       As Long = &H400
Private Const SV_TYPE_XENIX_SERVER        As Long = &H800
Private Const SV_TYPE_NT                  As Long = &H1000
Private Const SV_TYPE_WFW                 As Long = &H2000
Private Const SV_TYPE_SERVER_MFPN         As Long = &H4000
Private Const SV_TYPE_SERVER_NT           As Long = &H8000
Private Const SV_TYPE_POTENTIAL_BROWSER   As Long = &H10000
Private Const SV_TYPE_BACKUP_BROWSER      As Long = &H20000
Private Const SV_TYPE_MASTER_BROWSER      As Long = &H40000
Private Const SV_TYPE_DOMAIN_MASTER       As Long = &H80000
Private Const SV_TYPE_SERVER_OSF          As Long = &H100000
Private Const SV_TYPE_SERVER_VMS          As Long = &H200000
Private Const SV_TYPE_WINDOWS             As Long = &H400000  'Windows95 and above
Private Const SV_TYPE_DFS                 As Long = &H800000  'Root of a DFS tree
Private Const SV_TYPE_CLUSTER_NT          As Long = &H1000000 'NT Cluster
Private Const SV_TYPE_TERMINALSERVER      As Long = &H2000000 'Terminal Server
Private Const SV_TYPE_DCE                 As Long = &H10000000 'IBM DSS
Private Const SV_TYPE_ALTERNATE_XPORT     As Long = &H20000000 'rtn alternate transport
Private Const SV_TYPE_LOCAL_LIST_ONLY     As Long = &H40000000 'rtn local only
Private Const SV_TYPE_DOMAIN_ENUM         As Long = &H80000000
Private Const SV_TYPE_ALL                 As Long = &HFFFFFFFF

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

Private Const MAJOR_VERSION_MASK        As Long = &HF

Private Type SERVER_INFO_100
  sv100_platform_id As Long
  sv100_name As Long
End Type
Private Declare Function NetServerEnum Lib "netapi32" _
  (ByVal servername As Long, _
   ByVal level As Long, _
   buf As Any, _
   ByVal prefmaxlen As Long, _
   entriesread As Long, _
   totalentries As Long, _
   ByVal servertype As Long, _
   ByVal domain As Long, _
   resume_handle As Long) As Long

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

Private Declare Sub CopyMemory Lib "kernel32" _
   Alias "RtlMoveMemory" _
  (pTo As Any, uFrom As Any, _
   ByVal lSize As Long)
Private Declare Function lstrlenW Lib "kernel32" _
  (ByVal lpString As Long) As Long


Private Function GetServers(ByVal sDomain As String) As Long
On Error GoTo Last:

  'lists all servers of the specified type
  'that are visible in a domain.
   Dim bufptr          As Long
   Dim dwEntriesread   As Long
   Dim dwTotalentries  As Long
   Dim dwResumehandle  As Long
   Dim se100           As SERVER_INFO_100
   Dim success         As Long
   Dim nStructSize     As Long
   Dim cnt             As Long

   nStructSize = LenB(se100)
  success = NetServerEnum(0&, _
                           100, _
                           bufptr, _
                           MAX_PREFERRED_LENGTH, _
                           dwEntriesread, _
                           dwTotalentries, _
                           SV_TYPE_ALL, _
                           StrPtr(sDomain), _

  '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 one chunk of data and cast
        'into an SERVER_INFO_100 struct
        'in order to add the name to a list
         CopyMemory se100, ByVal bufptr + (nStructSize * cnt), nStructSize
'Listing Servers
     Debug.Print GetPointerToByteStringW(se100.sv100_name)
   End If
'clean up regardless of success
   Call NetApiBufferFree(bufptr)
  'return entries as sign of success
   GetServers = dwEntriesread
  Exit Function
Debug.Print "Enumerating Servers Failed"
End Function

Public Function GetPointerToByteStringW(ByVal dwData As Long) As String
   Dim tmp() As Byte
   Dim tmplen As Long
   If dwData <> 0 Then
      tmplen = lstrlenW(dwData) * 2
      If tmplen <> 0 Then
         ReDim tmp(0 To (tmplen - 1)) As Byte
         CopyMemory tmp(0), ByVal dwData, tmplen
         GetPointerToByteStringW = tmp
     End If
   End If
End Function
'Just call the function GetServers
' and check now all servers are get listed


LVL 10

Expert Comment

ID: 12367468
Not sure really, but you're not initiaslising lPreferedMaxLen, which, to ensure all are returned should be MAX_PREFERRED_LENGTH (-1). Have you checked the return values, and compared lEntriesRead to lTotalEntries? If your return value is ERROR_MORE_DATA then the buffer isn't large enough.
This link provides an example of NetServerEnum in VB from MSDN: http://support.microsoft.com/default.aspx?scid=kb;en-us;198896

Regards .. Alan
LVL 26

Expert Comment

ID: 12368846
Try this

Set myobj = GetObject("WinNT://yourdomainname")
myobj.Filter = Array("computer")
For Each comp In myobj
  Debug.Print comp.Name

Accepted Solution

modulo earned 0 total points
ID: 12624123
PAQed with no points refunded (of 500)

Community Support Moderator

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

597 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