Imports System.Runtime.InteropServices
Namespace Win32VB
Public Class NetApi32
Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal BufPtr As IntPtr) As Integer
Private Declare Unicode Function NetServerEnum Lib "netapi32" _
(ByVal Servername As IntPtr, _
ByVal Level As Integer, _
ByRef bufptr As IntPtr, _
ByVal PrefMaxLen As Integer, _
ByRef entriesread As Integer, _
ByRef TotalEntries As Integer, _
ByVal serverType As Integer, _
ByVal Domain As IntPtr, _
ByVal ResumeHandle As Integer) As Integer
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> _
Public Structure SERVER_INFO_101
Dim dwPlatformID As Integer
Dim lpszServerName As String
Dim dwVersionMajor As Integer
Dim dwVersionMinor As Integer
Dim dwType As Integer
Dim lpszComment As String
End Structure
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Integer) As Integer
'Windows type used to call the Net API
Private Const MAX_PREFERRED_LENGTH As Integer = -1
Private Const NERR_SUCCESS As Integer = 0
Private Const ERROR_MORE_DATA As Integer = 234
Private Const SV_TYPE_WORKSTATION As Integer = &H1S
Private Const SV_TYPE_SERVER As Integer = &H2S
Private Const SV_TYPE_SQLSERVER As Integer = &H4S
Private Const SV_TYPE_DOMAIN_CTRL As Integer = &H8S
Private Const SV_TYPE_DOMAIN_BAKCTRL As Integer = &H10S
Private Const SV_TYPE_TIME_SOURCE As Integer = &H20S
Private Const SV_TYPE_AFP As Integer = &H40S
Private Const SV_TYPE_NOVELL As Integer = &H80S
Private Const SV_TYPE_DOMAIN_MEMBER As Integer = &H100S
Private Const SV_TYPE_PRINTQ_SERVER As Integer = &H200S
Private Const SV_TYPE_DIALIN_SERVER As Integer = &H400S
Private Const SV_TYPE_XENIX_SERVER As Integer = &H800S
Private Const SV_TYPE_SERVER_UNIX As Integer = SV_TYPE_XENIX_SERVER
Private Const SV_TYPE_NT As Integer = &H1000S
Private Const SV_TYPE_WFW As Integer = &H2000S
Private Const SV_TYPE_SERVER_MFPN As Integer = &H4000S
Private Const SV_TYPE_SERVER_NT As Integer = &H8000S
Private Const SV_TYPE_POTENTIAL_BROWSER As Integer = &H10000
Private Const SV_TYPE_BACKUP_BROWSER As Integer = &H20000
Private Const SV_TYPE_MASTER_BROWSER As Integer = &H40000
Private Const SV_TYPE_DOMAIN_MASTER As Integer = &H80000
Private Const SV_TYPE_SERVER_OSF As Integer = &H100000
Private Const SV_TYPE_SERVER_VMS As Integer = &H200000
Private Const SV_TYPE_WINDOWS As Integer = &H400000 'Windows95 and above
Private Const SV_TYPE_DFS As Integer = &H800000 'Root of a DFS tree
Private Const SV_TYPE_CLUSTER_NT As Integer = &H1000000 'NT Cluster
Private Const SV_TYPE_TERMINALSERVER As Integer = &H2000000 'TerminalServer
Private Const SV_TYPE_DCE As Integer = &H10000000 'IBM DSS
Private Const SV_TYPE_ALTERNATE_XPORT As Integer = &H20000000 'rtnalternate transport
Private Const SV_TYPE_LOCAL_LIST_ONLY As Integer = &H40000000 'rtn localonly
Private Const SV_TYPE_DOMAIN_ENUM As Integer = &H80000000
Private Const SV_TYPE_ALL As Integer = &HFFFFFFFF
Private Const SV_PLATFORM_ID_OS2 As Integer = 400
Private Const SV_PLATFORM_ID_NT As Integer = 500
'Mask applied to svX_version_major in
'order to obtain the major version number.
Private Const MAJOR_VERSION_MASK As Integer = &HFS
<StructLayout(LayoutKind.Sequential)> Public Structure SERVER_INFO_100
Dim sv100_platform_id As Integer
Dim sv100_name As Integer
End Structure
Public Shared Function GetAllComputersInDomain() As ArrayList
Dim bufptr As IntPtr
Dim dwEntriesread As Integer
Dim dwTotalentries As Integer
Dim dwResumehandle As Integer
Dim se101 As SERVER_INFO_101 = New SERVER_INFO_101
Dim success As Integer
Dim nStructSize As Integer
Dim cnt As Integer
nStructSize = Marshal.SizeOf(se101)
success = NetServerEnum(IntPtr.Zero, 101, bufptr, MAX_PREFERRED_LENGTH, dwEntriesread, dwTotalentries, SV_TYPE_NT, IntPtr.Zero, dwResumehandle)
'if all goes well
Dim resSC As New ArrayList
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
se101 = DirectCast(Marshal.PtrToStructure(New IntPtr(bufptr.ToInt32 + (nStructSize * cnt)), GetType(SERVER_INFO_101)), SERVER_INFO_101)
resSC.Add(se101)
Next
End If
'clean up regardless of success
Call NetApiBufferFree(bufptr)
'return entries as sign of success
Return resSC
End Function
End Class
End Namespace