CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 1999
    Posts
    3,332

    getting all SQLServers in your domain

    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.


    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

    private Sub Command1_Click()
    Dim l as Long
    Dim entriesread as Long
    Dim totalentries as Long
    Dim hREsume as Long
    Dim bufptr as Long
    Dim level as Long
    level = 100
    Dim prefmaxlen as Long
    prefmaxlen = -1
    Dim lType as Long
    lType = SV_TYPE_SQLSERVER
    Dim domain() as Byte
    Dim sv100 as SV_100
    domain = "placeYourDomainNameHere" & vbNullChar
    l = NetServerEnum(byval 0&, _
    level, _
    bufptr, _
    prefmaxlen, _
    entriesread, _
    totalentries, _
    lType, _
    domain(0), _
    hREsume)
    If l = 0 Or l = 234& then
    Dim i as Long
    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




    (if you run that code within in your own network you might be as surprised as I was, when you find how many SQLServers are installed! :-))


  2. #2
    Join Date
    Apr 1999
    Location
    Netherlands
    Posts
    181

    Re: getting all SQLServers in your domain

    Hey tnx Lothar, nice job :-)

    Crazy D @ Work :-)

  3. #3
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: getting all SQLServers in your domain

    Nice one Lothar - want me to post it as an article on the site ?


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: getting all SQLServers in your domain

    go ahead, Chris.


  5. #5
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: getting all SQLServers in your domain

    Hi Lothar

    I've posted this one too :

    http://codeguru.developer.com/vb/articles/1916.shtml


    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured