Get a list of all SQL Servers in a specified Domain | CodeGuru

Get a list of all SQL Servers in a specified Domain

Author: Lothar Haensler Ever wanted to know which NT machines are used as SQLServers? When playing around with the NetServerEnum API I found the following solution that I just wanted to share. The code doesn’t return the list of servers, it simply does a ‘debug.print’ – it could be easily modified to return a disconnected […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 30, 2004
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Author: Lothar Haensler

Ever wanted to know which NT machines are used as SQLServers?

When playing around with the NetServerEnum API I found the following solution that I just wanted to share. The code doesn’t return the list of servers, it simply does a ‘debug.print’ – it could be easily modified to return a disconnected ADOR Recordset, an array or even a collection.

Paste the following code into a BAS module :

option Explicit
'
'
'
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()
'
' You could change this to be a function returning
' a list of the SQL servers in a ADOR Recordset or an array etc.
'
' At present, it just does a debug.print of all the
' SQL servers on the network.
'
'
    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 = "placeYourDomainNameHere" & 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
'
'

Download Bas Module (2k)

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.