Microsoft KB Archive/198896

From BetaArchive Wiki

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

Q198896



The information in this article applies to:


  • Microsoft Windows NT Server version 4.0
  • Microsoft Windows NT Workstation version 4.0
  • Microsoft Windows 2000 Advanced Server
  • Microsoft Windows 2000 Server
  • Microsoft Windows 2000 Professional





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 Command2 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 numbers below to view the articles 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

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 kbOSWinNT400 kbOSWin2000 kbSDKPlatform kbNetAPI _IK kbGrpDSNet
Issue type : kbhowto
Technology : kbWinNTsearch kbWinNTWsearch kbWinNTW400 kbWinNTW400search kbWinNT400xsearch kbwin2000AdvServ kbwin2000AdvServSearch kbwin2000Serv kbWinNTSsearch kbWinNTS400xsearch kbWinNTS400 kbwin2000ServSearch kbwin2000Search kbwin2000ProSearch kbwin2000Pro kbWinAdvServSearch


Last Reviewed: March 26, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.