Many thanks Marco.
Mark
-----Original Message-----
From: Marco Straforini [mailto:marco.straforini@c...]
Sent: 25 November 2002 22:21
To: professional vb
Subject: [pro_vb] Re: List all the computer names on the network
Mark,
in the code below (past it in a bas module) you have two public
methods, ServersNames returns the list af available
servers (I use it to returns the sql servers) and GetServerInfo
I modified something from someone, but so long ago... please
be careful, as usual, that email truncates long lines
Option Explicit
' General definitions
Const ERROR_SUCCESS = 0
Const ERROR_MORE_DATA = 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 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 NetServerGetInfo Lib "netapi32" ( _
ServerName As Byte, _
ByVal Level As Long, _
buffer 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
Public Function ServersNames(domain As String, servers() As String, _
Optional ByVal sqlonly As Boolean = False) As
Integer
Dim pszDomain As String
Dim nLevel As Long
Dim BufPtr As Long
Dim nPrefMaxLen As Long
Dim nEntriesRead As Long
Dim nTotalEntries As Long
Dim nServerType As Long
Dim nResumeHandle As Long
Dim nRes As Long
nLevel = 101
BufPtr = 0
nPrefMaxLen = &HFFFFFFFF
nEntriesRead = 0
nTotalEntries = 0
nResumeHandle = 0
If Len(domain) = 0 Then
pszDomain = vbNullString
Else
pszDomain = domain
End If
If sqlonly Then
nServerType = SV_TYPE_SQLSERVER
Else
nServerType = SV_TYPE_SERVER
End If
nRes = NetServerEnum(vbNullString, nLevel, BufPtr, _
nPrefMaxLen, nEntriesRead, nTotalEntries, _
nServerType, pszDomain, nResumeHandle)
If (nRes = ERROR_SUCCESS) And (nEntriesRead > 0) Then
Dim i As Long, TempBufPtr As Long
Dim ServerInfo As SERVER_INFO_101
Dim strSize As Long
strSize = LenB(ServerInfo)
TempBufPtr = BufPtr
ReDim servers(nTotalEntries - 1)
For i = 1 To nEntriesRead
RtlMoveMemory ServerInfo, TempBufPtr, strSize
servers(i - 1) = StrConv(PointerToString
(ServerInfo.lpszServerName), vbProperCase)
TempBufPtr = TempBufPtr + strSize
Next i
Else
nTotalEntries = 0
End If
NetApiBufferFree (BufPtr)
ServersNames = nTotalEntries
End Function
Public Sub GetServerInfo(server As String, comment As String)
Dim nRes As Long
Dim buf As Long
Dim srv() As Byte
Dim info As SERVER_INFO_101
srv = "\\" & server & vbNullChar
nRes = NetServerGetInfo(srv(0), 101, buf)
If nRes = ERROR_SUCCESS Then
RtlMoveMemory info, ByVal buf, LenB(info)
comment = PointerToString(info.lpszComment)
Else
comment = ""
End If
End Sub
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