Results 1 to 6 of 6

Thread: List of Servers using NetServerEnum

  1. #1

    Thread Starter
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016

    List of Servers using NetServerEnum

    I have a function that returns a list of servers on our network by utilizing the NetServerEnum API call. The only problem is that the NetServerEnum seems to be picking up certain PC's as well. Is there anyway to utilize the API in such a way that it distinguishes between a NT/2000 server and a NT/2000 workstation?

    Thanks
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Sure. It is possible only in NT/2000 environment.
    If you're running under NT or 2000 then you can do this. Add a Listbox (List1), Command Button (Command1) and a Label (Label1):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function NetServerEnum Lib "Netapi32.dll" (vServername As Any, ByVal lLevel As Long, vBufptr As Any, lPrefmaxlen As Long, lEntriesRead As Long, lTotalEntries As Long, vServerType As Any, ByVal sDomain As String, vResumeHandle As Any) As Long
    4. Private Declare Sub RtlMoveMemory Lib "kernel32" (dest As Any, vSrc As Any, ByVal lSize As Long)
    5. Private Declare Sub lstrcpyW Lib "kernel32" (vDest As Any, ByVal sSrc As Any)
    6. Private Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long
    7. Private Type SERVER_INFO_101
    8.     dw_platform_id As Long
    9.     ptr_name As Long
    10.     dw_ver_major As Long
    11.     dw_ver_minor As Long
    12.     dw_type As Long
    13.     ptr_comment As Long
    14. End Type
    15. Private Const SV_TYPE_SQLSERVER = &H4
    16. Private Const SV_TYPE_SERVER As Long = &H2
    17.  
    18. Private Type OSVERSIONINFO
    19.         dwOSVersionInfoSize As Long
    20.         dwMajorVersion As Long
    21.         dwMinorVersion As Long
    22.         dwBuildNumber As Long
    23.         dwPlatformId As Long
    24.         szCSDVersion As String * 128
    25. End Type
    26. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
    27. Private Const VER_PLATFORM_WIN32_NT = 2
    28. Private Const VER_PLATFORM_WIN32_WINDOWS = 1
    29. Private Const VER_PLATFORM_WIN32s = 0
    30.  
    31.  
    32. Private Type WKSTA_INFO_101
    33.    wki101_platform_id As Long
    34.    wki101_computername As Long
    35.    wki101_langroup As Long
    36.    wki101_ver_major As Long
    37.    wki101_ver_minor As Long
    38.    wki101_lanroot As Long
    39. End Type
    40.  
    41. Private Type WKSTA_USER_INFO_1
    42.    wkui1_username As Long
    43.    wkui1_logon_domain As Long
    44.    wkui1_logon_server As Long
    45.    wkui1_oth_domains As Long
    46. End Type
    47. Private Declare Function NetWkstaGetInfo Lib "Netapi32" (strServer As Any, ByVal lLevel As Long, pbBuffer As Any) As Long
    48. Private Declare Function NetWkstaUserGetInfo Lib "Netapi32" (reserved As Any, ByVal lLevel As Long, pbBuffer As Any) As Long
    49.  
    50. Function GetDomainName()
    51.     Dim lngRet As Long
    52.     Dim arrByteBuffer(512) As Byte
    53.     Dim i As Integer
    54.     Dim tWK_INFO As WKSTA_INFO_101
    55.     Dim lngWK_Ptr As Long
    56.     Dim tWK_USER As WKSTA_USER_INFO_1
    57.     Dim lngWK_USER_Ptr As Long
    58.     Dim strDomain As String
    59.  
    60.     If IfWinNT Then
    61.         lngRet = NetWkstaGetInfo(ByVal 0&, 101, lngWK_Ptr)
    62.         RtlMoveMemory tWK_INFO, ByVal VarPtr(tWK_INFO), Len(tWK_INFO)
    63.         lngRet = NetWkstaUserGetInfo(ByVal 0&, 1, lngWK_USER_Ptr)
    64.         RtlMoveMemory tWK_USER, ByVal lngWK_USER_Ptr, Len(tWK_USER)
    65.         lstrcpyW arrByteBuffer(0), tWK_USER.wkui1_logon_domain
    66.         'Get Every other byte of the array
    67.         i = 0
    68.         Do While arrByteBuffer(i) <> 0
    69.             strDomain = strDomain & Chr(arrByteBuffer(i))
    70.             i = i + 2
    71.         Loop
    72.         lngRet = NetApiBufferFree(lngWK_USER_Ptr)
    73.  
    74.         GetDomainName = strDomain
    75.     Else
    76.         GetDomainName = ""
    77.     End If
    78. End Function
    79.  
    80. Private Function IfWinNT() As Boolean
    81.     Dim os As OSVERSIONINFO
    82.     Dim lngRet As Long
    83.    
    84.     os.dwOSVersionInfoSize = Len(os)
    85.     lngRet = GetVersionEx(os)
    86.    
    87.     If lngRet <> 0 Then
    88.         Select Case os.dwPlatformId
    89.             Case VER_PLATFORM_WIN32_NT
    90.                 IfWinNT = True
    91.             Case Else
    92.                 IfWinNT = False
    93.         End Select
    94.     End If
    95. End Function
    96.  
    97.  
    98.  
    99. Private Sub Command1_Click()
    100.     Dim lngRet As Long
    101.     Dim lngServer_Info As Long
    102.     Dim lngEntries As Long
    103.     Dim lngTotal As Long
    104.     Dim lngMax As Long
    105.     Dim varResume As Variant
    106.     Dim tServer_info_101 As SERVER_INFO_101
    107.     Dim strServer As String
    108.     Dim strDomain As String
    109.     Dim lngServerInfo101StructPtr As Long
    110.     Dim lngCount As Long
    111.     Dim i As Long
    112.     Dim arrByteBuffer(512) As Byte
    113.    
    114.    
    115.    
    116.     If Not IfWinNT Then
    117.         MsgBox "You're not running under Window NT/2000."
    118.     End If
    119.    
    120.    
    121.     strDomain = GetDomainName
    122.    
    123.    
    124.     lngRet = NetServerEnum(ByVal 0&, 101, lngServer_Info, lngMax, _
    125.                             lngEntries, lngTotal, _
    126.                             ByVal SV_TYPE_SERVER, StrConv(strDomain, vbUnicode), varResume)
    127.  
    128.     If lngRet <> 0 Then
    129.         MsgBox "Error Getting Servers."
    130.         Exit Sub
    131.     End If
    132.  
    133.     lngCount = 1
    134.     lngServerInfo101StructPtr = lngServer_Info
    135.  
    136.     Do While lngCount <= lngTotal
    137.         RtlMoveMemory tServer_info_101, ByVal lngServerInfo101StructPtr, Len(tServer_info_101)
    138.  
    139.         lstrcpyW arrByteBuffer(0), tServer_info_101.ptr_name
    140.  
    141.         i = 0
    142.         Do While arrByteBuffer(i) <> 0
    143.             strServer = strServer & Chr(arrByteBuffer(i))
    144.             i = i + 2
    145.         Loop
    146.        
    147.         If Len(Trim(strServer)) > 0 Then List1.AddItem strServer
    148.        
    149.         lngCount = lngCount + 1
    150.         strServer = ""
    151.         lngServerInfo101StructPtr = lngServerInfo101StructPtr + Len(tServer_info_101)
    152.     Loop
    153.  
    154.     lngRet = NetApiBufferFree(lngServer_Info)
    155.    
    156.    
    157.     Label1.Caption = "There are " & lngTotal & " NT/2000 Servers in Domain: " & strDomain
    158. End Sub

  3. #3
    jim mcnamara
    Guest
    Are you calling it like this?

    Code:
    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
    
    NetServerEnum(0&, _
                               100, _
                               bufptr, _
                               MAX_PREFERRED_LENGTH, _
                               dwEntriesread, _
                               dwTotalentries, _
                               SV_TYPE_SERVER or SV_TYPE_DOMAIN_CTRL, _
                               0&, _
                               dwResumehandle)

  4. #4

    Thread Starter
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    Jim,

    Here's how I call:
    VB Code:
    1. Public Const SV_TYPE_SERVER    As Long = &H2
    2. Public Const SV_TYPE_DOMAIN_CTRL As Long = &H8
    3. Public Const SV_TYPE_SQLSERVER As Long = &H4
    4.  
    5. Public Sub GetServers()
    6. On Error GoTo errHandler
    7.  
    8.     Dim L As Long
    9.     Dim EntriesRead As Long
    10.     Dim TotalEntries As Long
    11.     Dim hResume As Long
    12.     Dim bufPtr As Long
    13.     Dim Level As Long
    14.     Dim PrefMaxLen As Long
    15.     Dim lType As Long
    16.     Dim Domain() As Byte
    17.     Dim I As Long
    18.     Dim sv100 As SV_100
    19.     Dim strDomain As String
    20.     Dim svrName As String
    21.    
    22.     Level = 100
    23.     PrefMaxLen = -1
    24.     lType = SV_TYPE_SERVER
    25.    
    26.     Domain = "MyDomain" & vbNullChar
    27.      
    28.     L = NetServerEnum(ByVal 0&, _
    29.                   Level, _
    30.                   bufPtr, _
    31.                   PrefMaxLen, _
    32.                   EntriesRead, _
    33.                   TotalEntries, _
    34.                   SV_TYPE_SERVER Or SV_TYPE_DOMAIN_CTRL, _
    35.                   Domain(0), _
    36.                   hResume)
    37.     ...
    38. End Sub
    I added the SV_TYPE_DOMAIN_CTRL, and I still get both returned. All I want to see are the actual servers.

    Serge,

    I'll try you solution out later and try to filter out all of the workstations.

    Thanks,

    Chris
    Chris

    Master Of My Domain
    Got A Question? Look Here First

  5. #5
    jim mcnamara
    Guest
    You are in a Win 2000 or NT network, right?

  6. #6

    Thread Starter
    Fanatic Member vb_dba's Avatar
    Join Date
    Jun 2001
    Location
    Somewhere aloft between the real world and insanity
    Posts
    1,016
    We are running an NT network.

    If I look at the Network Neighborhood for our Domain, I see all of these pc's listed, along with the servers. I'm not that familiar w/ networks, so could there be something wrong with the way the pc's are setup on the network?

    Chris
    Chris

    Master Of My Domain
    Got A Question? Look Here First

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width