jas_alien2
Sep 24th, 2000, 10:10 PM
is it possible to find out all the list of sql servers using vb????
monte96
Sep 24th, 2000, 10:39 PM
I know it's possible cuz I've seen Serge do it...
jas_alien2
Sep 24th, 2000, 11:01 PM
but how??? can u or your friend give me a sample code?? pls help
Serge
Sep 25th, 2000, 02:26 PM
It is possible only in NT/2000 environment.
If you're running under NT or 2000 then you can do this. Add a Listbox (List1), Command Button (Command1) and a Label (Label1):
Private Declare Function NetServerEnum Lib "Netapi32.dll" (vServername As Any, ByVal lLevel As Long, vBufptr As Any, lPrefmaxlen As Long, lEntriesRead As Long, lTotalEntries As Long, vServerType As Any, ByVal sDomain As String, vResumeHandle As Any) As Long
Private Declare Sub RtlMoveMemory Lib "kernel32" (dest As Any, vSrc As Any, ByVal lSize As Long)
Private Declare Sub lstrcpyW Lib "kernel32" (vDest As Any, ByVal sSrc As Any)
Private Declare Function NetApiBufferFree Lib "Netapi32.dll" (ByVal lpBuffer As Long) As Long
Private Type SERVER_INFO_101
dw_platform_id As Long
ptr_name As Long
dw_ver_major As Long
dw_ver_minor As Long
dw_type As Long
ptr_comment As Long
End Type
Private Const SV_TYPE_SQLSERVER = &H4
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Private Const VER_PLATFORM_WIN32_NT = 2
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VER_PLATFORM_WIN32s = 0
Private Type WKSTA_INFO_101
wki101_platform_id As Long
wki101_computername As Long
wki101_langroup As Long
wki101_ver_major As Long
wki101_ver_minor As Long
wki101_lanroot As Long
End Type
Private Type WKSTA_USER_INFO_1
wkui1_username As Long
wkui1_logon_domain As Long
wkui1_logon_server As Long
wkui1_oth_domains As Long
End Type
Private Declare Function NetWkstaGetInfo Lib "Netapi32" (strServer As Any, ByVal lLevel As Long, pbBuffer As Any) As Long
Private Declare Function NetWkstaUserGetInfo Lib "Netapi32" (reserved As Any, ByVal lLevel As Long, pbBuffer As Any) As Long
Function GetDomainName()
Dim lngRet As Long
Dim arrByteBuffer(512) As Byte
Dim i As Integer
Dim tWK_INFO As WKSTA_INFO_101
Dim lngWK_Ptr As Long
Dim tWK_USER As WKSTA_USER_INFO_1
Dim lngWK_USER_Ptr As Long
Dim strDomain As String
If IfWinNT Then
lngRet = NetWkstaGetInfo(ByVal 0&, 101, lngWK_Ptr)
RtlMoveMemory tWK_INFO, ByVal tWK_INFO, Len(tWK_INFO)
lngRet = NetWkstaUserGetInfo(ByVal 0&, 1, lngWK_USER_Ptr)
RtlMoveMemory tWK_USER, ByVal lngWK_USER_Ptr, Len(tWK_USER)
lstrcpyW arrByteBuffer(0), tWK_USER.wkui1_logon_domain
'Get Every other byte of the array
i = 0
Do While arrByteBuffer(i) <> 0
strDomain = strDomain & Chr(arrByteBuffer(i))
i = i + 2
Loop
lngRet = NetApiBufferFree(lngWK_USER_Ptr)
GetDomainName = strDomain
Else
GetDomainName = ""
End If
End Function
Private Function IfWinNT() As Boolean
Dim os As OSVERSIONINFO
Dim lngRet As Long
os.dwOSVersionInfoSize = Len(os)
lngRet = GetVersionEx(os)
If lngRet <> 0 Then
Select Case os.dwPlatformId
Case VER_PLATFORM_WIN32_NT
IfWinNT = True
Case Else
IfWinNT = False
End Select
End If
End Function
Private Sub Command1_Click()
Dim lngRet As Long
Dim Server_Info As Long
Dim lngEntries As Long
Dim lngTotal As Long
Dim lngMax As Long
Dim varResume As Variant
Dim tServer_info_101 As SERVER_INFO_101
Dim strServer As String
Dim strDomain As String
Dim lngServerInfo101StructPtr As Long
Dim lngCount As Long
Dim i As Long
Dim arrByteBuffer(512) As Byte
If Not IfWinNT Then
MsgBox "You're not running under Window NT/2000."
End If
strDomain = GetDomainName
lngRet = NetServerEnum(ByVal 0&, 101, Server_Info, lngMax, _
lngEntries, lngTotal, _
ByVal SV_TYPE_SQLSERVER, StrConv(strDomain, vbUnicode), varResume)
If lngRet <> 0 Then
MsgBox "Error Getting SQL Servers."
Exit Sub
End If
lngCount = 1
lngServerInfo101StructPtr = Server_Info
Do While lngCount <= lngTotal
RtlMoveMemory tServer_info_101, ByVal lngServerInfo101StructPtr, Len(tServer_info_101)
lstrcpyW arrByteBuffer(0), tServer_info_101.ptr_name
i = 0
Do While arrByteBuffer(i) <> 0
strServer = strServer & Chr(arrByteBuffer(i))
i = i + 2
Loop
If Len(Trim(strServer)) > 0 Then List1.AddItem strServer
lngCount = lngCount + 1
strServer = ""
lngServerInfo101StructPtr = lngServerInfo101StructPtr + Len(tServer_info_101)
Loop
lngRet = NetApiBufferFree(Server_Info)
Label1.Caption = "There are " & lngTotal & " SQL Servers in Domain: " & strDomain
End Sub