Userlist

Posts 1–2 of 2 · Page 1 of 1
Userlist
hi mpgh
i have another question

how can i add all users in a network to a listbox?
thanks
Code:
#Region "Declarations"

    Private Declare Unicode Function NetServerEnum Lib "netapi32" _
    (ByVal Servername As IntPtr, _
    ByVal Level As Integer, _
    ByRef bufptr As IntPtr, _
    ByVal PrefMaxLen As Integer, _
    ByRef entriesread As Integer, _
    ByRef TotalEntries As Integer, _
    ByVal serverType As Integer, _
    ByVal Domain As IntPtr, _
    ByVal ResumeHandle As IntPtr) As Integer

    Declare Unicode Function NetApiBufferFree Lib "netapi32.dll" _
    (ByRef buffer As IntPtr) As Long

    Public Structure _SERVER_INFO_100
        Friend sv100_platform_id As Integer
        <MarshalAs(UnmanagedType.LPWStr)> _
        Friend sv100_name As String
    End Structure

#End Region

#Region "Get network computers"
    Public Function GetNetworkComputers() As ArrayList
        Dim networkComputers As New ArrayList()
        Const MAX_PREFERRED_LENGTH As Integer = -1
        Dim SV_TYPE_WORKSTATION As Integer = 1
        Dim SV_TYPE_SERVER As Integer = 2
        Dim buffer As IntPtr = IntPtr.Zero
        Dim tmpBuffer As IntPtr = IntPtr.Zero

        Dim entriesRead As Integer = 0
        Dim totalEntries As Integer = 0
        Dim resHandle As Integer = 0
        Dim sizeofINFO As Integer = Marshal.SizeOf(GetType(_SERVER_INFO_100))

        Try
            Dim ret As Integer = NetServerEnum(Nothing, 100, buffer, MAX_PREFERRED_LENGTH, entriesRead, totalEntries, _
                                               SV_TYPE_WORKSTATION Or SV_TYPE_SERVER, Nothing, resHandle)

            If ret = 0 Then
                For i As Integer = 0 To totalEntries
                    tmpBuffer = New IntPtr(CType(buffer, Integer) + (i * sizeofINFO))
                    Dim svrInfo As _SERVER_INFO_100 = CType(Marshal.PtrToStructure(tmpBuffer, _
                                                            GetType(_SERVER_INFO_100)), _SERVER_INFO_100)

                    networkComputers.Add(svrInfo.sv100_name)
                Next
            End If
        Catch ex As Exception
            Console.WriteLine("Error getting network computers.")
        Finally
            NetApiBufferFree(buffer)
        End Try

        Return networkComputers
    End Function
#End Region

' Example Usage
Dim networkComps As ArrayList = GetNetworkComputers()
For Each Str As String In networkComps
    Console.WriteLine(Str)
Next
Credits: RodgerB

I haven't tried it myself though
Posts 1–2 of 2 · Page 1 of 1

Post a Reply

Tags for this Thread

None

Talk with us