HOWTO: List Servers in 32-bit Visual Basic Using NetServerEnum

ID: Q198896


The information in this article applies to:
  • Microsoft Windows NT 4.0
  • Microsoft Windows 2000


SUMMARY

This article includes a sample which demonstrates how to enumerate all servers of the specified type that are visible in the specified domain in a 32-bit Visual Basic application using NetServerEnum().

Note that the 32-bit version of NetServerEnum() is only supported in Windows NT. The function uses UNICODE for its string parameters.


MORE INFORMATION

Note when calling C functions in Visual Basic (VB), the relevant constants, structures(types), and functional prototypes have to be redefined or declared. This example shows how to use NetServerEnum() at level 101 to enumerate all types of visible servers in a domain. Therefore, you must define the filtering flag SV_TYPE_SERVER and the structure SERVER_INFO_101 based on the C header file Lmserver.h. You also have to declare the prototypes for NetServerEnum() and the memory management functions such as NetApiBufferFree(), RtlMoveMemory(), and lstrcpyW().

The sample application contains only two buttons, Command1 and Command2. Command1 is for executing NetServerEnum() and Command1 is used to exit the application.


   Option Explicit

   ' General definitions
   Const ERROR_SUCCESS = 0
   Const ERROR_MORE_DATA = 234
   Const SV_TYPE_SERVER = &H2   'Server type mask, all types of servers
   Const SIZE_SI_101 = 24

   Private Type SERVER_INFO_101
      dwPlatformId As Long
      lpszServerName As Long
      dwVersionMajor As Long
      dwVersionMinor As Long
      dwType As Long
      lpszComment As Long
   End Type

   Private Declare Function NetServerEnum Lib "netapi32.dll" ( _
      ByVal servername As String, _
      ByVal level As Long, _
      buffer As Long, _
      ByVal prefmaxlen As Long, _
      entriesread As Long, _
      totalentries As Long, _
      ByVal servertype As Long, _
      ByVal domain As String, _
      resumehandle As Long) As Long

   Private Declare Function NetApiBufferFree Lib "netapi32.dll" ( _
      BufPtr As Any) As Long
   Private Declare Sub RtlMoveMemory Lib "KERNEL32" ( _
      hpvDest As Any, ByVal hpvSource As Long, ByVal cbCopy As Long)
   Private Declare Function lstrcpyW Lib "KERNEL32" ( _
      ByVal lpszDest As String, ByVal lpszSrc As Long) As Long

   Private Function PointerToString(lpszString As Long) As String
      Dim lpszStr1 As String, lpszStr2 As String, nRes As Long
      lpszStr1 = String(1000, "*")
      nRes = lstrcpyW(lpszStr1, lpszString)
      lpszStr2 = (StrConv(lpszStr1, vbFromUnicode))
      PointerToString = Left(lpszStr2, InStr(lpszStr2, Chr$(0)) - 1)
   End Function

   Private Sub Command1_Click()
      Dim pszTemp As String, pszServer As String, pszDomain As String
      Dim nLevel As Long, i As Long, BufPtr As Long, TempBufPtr As Long
      Dim nPrefMaxLen As Long, nEntriesRead As Long, nTotalEntries As Long
      Dim nServerType As Long, nResumeHandle As Long, nRes As Long
      Dim ServerInfo As SERVER_INFO_101

      ' Get the server name. It can be a null string
      pszTemp = Chr(0)
      pszTemp = InputBox("Enter server name:", "Server Name")
      If Len(pszTemp) = 0 Then
         pszServer = vbNullString
      Else
         pszServer = StrConv(pszTemp, vbUnicode)
      End If

      ' Get the domain name. It can be a null string
      pszTemp = Chr(0)
      pszTemp = InputBox("Enter domain name:", "Domain Name")
      If Len(pszTemp) = 0 Then
         pszDomain = vbNullString
      Else
         pszDomain = StrConv(pszTemp, vbUnicode)
      End If

      nLevel = 101
      BufPtr = 0
      nPrefMaxLen = &HFFFFFFFF
      nEntriesRead = 0
      nTotalEntries = 0
      nServerType = SV_TYPE_SERVER
      nResumeHandle = 0

      Do
         nRes = NetServerEnum(pszServer, nLevel, BufPtr, _
                         nPrefMaxLen, nEntriesRead, nTotalEntries, _
                         nServerType, pszDomain, nResumeHandle)
         If ((nRes = ERROR_SUCCESS) Or (nRes = ERROR_MORE_DATA)) And _
            (nEntriesRead > 0) Then
            TempBufPtr = BufPtr
            For i = 1 To nEntriesRead
               RtlMoveMemory ServerInfo, TempBufPtr, SIZE_SI_101
               Debug.Print PointerToString(ServerInfo.lpszServerName)
               TempBufPtr = TempBufPtr + SIZE_SI_101
            Next i
         Else
            MsgBox "NetServerEnum failed: " & nRes
         End If
         NetApiBufferFree (BufPtr)
      Loop While nEntriesRead < nTotalEntries

   End Sub

   Private Sub Command2_Click()
      Unload Me
   End Sub
 


REFERENCES

For additional information, please click the article number(s) below to view the article(s) in the Microsoft Knowledge Base:

Q159498 HOWTO: Call LanMan Services from 32-bit Visual Basic Apps
Q159423 HOWTO: Call LAN Manager Functions from 16-bit Visual Basic 4.0
Q151774 HOWTO: Call NetUserGetInfo API from Visual Basic
Q106553 HOWTO: Write C DLLs and Call Them from Visual Basic
Q118643 How to Pass a String or String Arrays Between VB and a C DLL
Q110219 LONG: How to Call Windows API from VB 3.0--General Guidelines
Q145647 FAQ: APIs and DLLs in Visual Basic 4.0
For more information on Active Directory Services Interfaces and ADSI interface IADsComputer, please see the MSDN Web Workshop:
http:://msdn.microsoft.com/workshop/default.asp

Additional query words: kbDSupport

Keywords : kbnetwork kbAPI kbNTOS400 kbWinOS2000 kbSDKPlatform kbNetAPI kbDSupport kbGrpNet
Version : WINDOWS:; winnt:4.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: January 31, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.