List of computers (VB2005)

Can you help me to find a method to discover the list of computers from my workgroup and i put the name of computers in to a listbox?

Thanks

[149 byte] By [FanelSAVA] at [2007-12-17]
# 1

You can use the netserverenum api to list the computers



Imports System.Runtime.InteropServices

Public Class Form1
<DllImport("netapi32.dll", EntryPoint:="NetServerEnum")> _
Public Shared Function NetServerEnum( _
<MarshalAs(UnmanagedType.LPWStr)> ByVal ServerName As String, _
ByVal level As Integer, _
<Out()> ByRef bufptr As IntPtr, _
ByVal PrefMaxLen As Integer, _
ByRef EntriesRead As Integer, _
ByRef TotalEntries As Integer, _
ByVal ServerType As Integer, _
<MarshalAs(UnmanagedType.LPWStr)> ByVal DomainName As String, _
ByRef ResumeHandle As Integer) As Integer
End Function

Structure Computer_info_101
Public Platform_ID As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Name As String
Public Version_Major As Integer
Public Version_Minor As Integer
Public Type As Integer
<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Comment As String
End Structure

Declare Function NetApiBufferFree Lib "Netapi32.dll" _
(ByVal lpBuffer As IntPtr) As Integer

Private Const SV_TYPE_SERVER As Integer = &H2 ' All Servers
Private Const SV_TYPE_SQLSERVER As Integer = &H4


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ComputerInfo As Computer_info_101
Dim i, MaxLenPref, level, ret, EntriesRead, TotalEntries, ResumeHandle As Integer
Dim BufPtr As IntPtr
Dim strServerName As String = "MSHome" 'Replace mshome with your network name

MaxLenPref = -1
level = 101
ret = NetServerEnum(Nothing, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries, _
SV_TYPE_SERVER, strServerName, ResumeHandle)
If ret <> 0 Then
ListBox1.Items.Add("An Error has occured " & ret.ToString)
Return
End If


' loop thru the entries
For i = 0 To EntriesRead - 1
' copy the stuff into our structure

ComputerInfo = CType(Marshal.PtrToStructure(BufPtr, GetType(Computer_info_101)), _
Computer_info_101)
If (IntPtr.Size = 4) Then
BufPtr = New IntPtr(BufPtr.ToInt32() + Marshal.SizeOf(ComputerInfo))
Else
BufPtr = New IntPtr(BufPtr.ToInt64() + Marshal.SizeOf(ComputerInfo))
End If

ListBox1.Items.Add(ComputerInfo.Name)

Next
NetApiBufferFree(BufPtr)
End Sub
End Class

KenTucker at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2
Thank you very much, Ken!

FanelSAVA at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...