Solved

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

Posted on 2004-10-20
6
359 Views
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
            Loop
           
            Combo_SQLServer.AddItem sComputerName
           
            'Debug.Print "Found server: " & sComputerName
            sComputerName = ""
            x = x + 1
           
            lSeverInfo101StructPtr = lSeverInfo101StructPtr + Len(tSeverInfo101)
         
         Loop
       
         lReturnCode = NetApiBufferFree(lSeverInfo101)
 
End Function
0
Comment
Question by:limct
6 Comments
 
LVL 3

Expert Comment

by:Noel_Castillo
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. :)
0
 
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_SERVER_UNIX         As Long = SV_TYPE_XENIX_SERVER
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), _
                           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 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)
     Next
   End If
'clean up regardless of success
   Call NetApiBufferFree(bufptr)
   
  'return entries as sign of success
   GetServers = dwEntriesread
  Exit Function
Last:
Debug.Print "Enumerating Servers Failed"
Err.Clear
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

;-)
Shiju



0
 
LVL 10

Expert Comment

by:ADSaunders
ID: 12367468
Hi,
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
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 12368846
Try this

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

Accepted Solution

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

modulo
Community Support Moderator
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

760 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

20 Experts available now in Live!

Get 1:1 Help Now