Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Declare Function NetServerEnum Lib "netapi32" (strServername As Any, ByVal level As Long, bufptr As Long, ByVal prefmaxlen As Long, entriesread As Long, totalentries As Long, ByVal servertype As Long, strDomain As Any, resumehandle As Long) As Long
Private Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Const SV_TYPE_SERVER As Long = &H2
Private Const SV_TYPE_SQLSERVER As Long = &H4
Private Type SV_100
platform As Long
name As Long
End Type
Public Sub GetSQLServers()
Dim l As Long
Dim entriesread As Long
Dim totalentries As Long
Dim hREsume As Long
Dim bufptr As Long
Dim level As Long
Dim prefmaxlen As Long
Dim lType As Long
Dim domain() As Byte
Dim i As Long
Dim sv100 As SV_100
level = 100
prefmaxlen = -1
lType = SV_TYPE_SQLSERVER
domain = "dltech" & vbNullChar
l = NetServerEnum(ByVal 0&, level, bufptr, prefmaxlen, entriesread, totalentries, lType, domain(0), hREsume)
If l = 0 Or l = 234& Then
For i = 0 To entriesread - 1
CopyMemory sv100, ByVal bufptr, Len(sv100)
Debug.Print Pointer2stringw(sv100.name)
bufptr = bufptr + Len(sv100)
Next i
End If
NetApiBufferFree bufptr
End Sub
Private Function Pointer2stringw(ByVal l As Long) As String
Dim buffer() As Byte
Dim nLen As Long
nLen = lstrlenW(l) * 2
If nLen Then
ReDim buffer(0 To (nLen - 1)) As Byte
CopyMemory buffer(0), ByVal l, nLen
Pointer2stringw = buffer
End If
End Function