Servernames in combobox

boes

Member
Joined
Jun 21, 2002
Messages
17
Location
Belgium
Hey,

In my application I want to make a list of all the servers names that are available in my network. What code do I need to put all those names into a combobox?
 
Heres some code to put all of your NT/2000 Servernames into a text file.

It may help you.

Code:
#Region "Add Domain Servers To Text File"

    Public Function GetNetList(ByVal DomainName As String) As String()
        Dim ServerInfo As New Server_info_101()
        Dim i, MaxLenPref, level, ret, EntriesRead, TotalEntries, _
            ResumeHandle As Integer
        Dim BufPtr As New IntPtr()
        Dim iPtr As IntPtr
        Dim tempPtr As Integer
        Dim ans(0) As String
        Dim sw As StreamWriter

        If File.Exists(Application.StartupPath & "\Servers.txt") = True Then
            Dim sr1 As StreamReader = File.OpenText(Application.StartupPath & _
                "\Servers.txt")
            Do Until sr1.Peek = -1
                frmMoveUser.lstboxServers.Items.Add(sr1.ReadLine)
            Loop
            Exit Function
        End If

        sw = File.CreateText(Application.StartupPath & "\Servers.txt")

        Const SV_TYPE_DOMAIN_CTRL As Long = &H8
        Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10


         lets do it!
        MaxLenPref = -1
        level = 101
        ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, _
            TotalEntries, SV_TYPE_ALL, DomainName, ResumeHandle)
        If ret <> 0 Then
            MsgBox("Hmmmm... something went wrong", vbCritical, "Error!")
            Return ans
        End If

        Dim nSize As Integer = Marshal.SizeOf(ServerInfo)
        ReDim ans(EntriesRead - 1)
        tempPtr = BufPtr.ToInt32

         loop thru the entries

        For i = 0 To EntriesRead - 1
             copy the stuff into our structure

            iPtr = New IntPtr(BufPtr.ToInt32 + i * nSize)

            ServerInfo = CType(Marshal.PtrToStructure(iPtr, _
                GetType(Server_info_101)), Server_info_101)

             Output ServerName to our text file.

            If ServerInfo.Type = 266291 Or _
                    ServerInfo.Type = 266259 Or _
                    ServerInfo.Type = 102435 Or _
                    ServerInfo.Type = 102403 Then
                sw.WriteLine(ServerInfo.Name)
            End If

        Next

        NetApiBufferFree(iPtr)
        NetApiBufferFree(BufPtr)
        sw.Close()

        Dim sr As StreamReader = File.OpenText(Application.StartupPath & _
                "\Servers.txt")
        Do Until sr.Peek = -1
            frmMoveUser.lstboxServers.Items.Add(sr.ReadLine)
        Loop

        sr.Close()

    End Function
#End Region

If you want though, you can substitute the following lines:-
Code:
If ServerInfo.Type = 266291 Or _
                    ServerInfo.Type = 266259 Or _
                    ServerInfo.Type = 102435 Or _
                    ServerInfo.Type = 102403 Then
                sw.WriteLine(ServerInfo.Name)
            End If

...with...

Code:
If ServerInfo.Type = 266291 Or _
                    ServerInfo.Type = 266259 Or _
                    ServerInfo.Type = 102435 Or _
                    ServerInfo.Type = 102403 Then
                myComboBox.Items.Add(ServerInfo.Name)
            End If

...to add the server to your combo box directly.
HTH. :)
 
Hey LeeSalter,

thanks for your code! Sorry it took so long to give a reply. I had some serious computer problems.
Can you tell me what those numbers stand for when you write for example the code ServerInfo.Type = 266259. What does 266259 mean?
 
Hi Lee,

i still have some problems,

my environment doesnt know keywords like

Server_info_101()
Application.StartupPath
SV_TYPE_ALL
NetServerEnum
Marshal
NetApiBufferFree

did i forget some classes?

THX

Schorschii
 
Back
Top