By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,900 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,900 IT Pros & Developers. It's quick & easy.

Enumerating machines on domain network

P: n/a
Hi - I found the class below on another website (www.devx.com)
and can't seem to figure out how to make a call to this class to
retrieve the machines and put them in a listbox.

I am definitely a beginner in vb programming, and would really
appreciate the help.

TIA!

Imports System.Runtime.InteropServices

Public Class MasterBrowseList

Private Const MAX_PREFERRED_LENGTH As Integer = -1&
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
Private Const SV_TYPE_WORKSTATION As Long = &H1
Private Const SV_TYPE_SERVER As Long = &H2
Private Const SV_TYPE_SQLSERVER As Long = &H4
Private Const SV_TYPE_DOMAIN_CTRL As Long = &H8
Private Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10
Private Const SV_TYPE_TIME_SOURCE As Long = &H20
Private Const SV_TYPE_AFP As Long = &H40
Private Const SV_TYPE_NOVELL As Long = &H80
Private Const SV_TYPE_DOMAIN_MEMBER As Long = &H100
Private Const SV_TYPE_PRINTQ_SERVER As Long = &H200
Private Const SV_TYPE_DIALIN_SERVER As Long = &H400
Private Const SV_TYPE_XENIX_SERVER As Long = &H800
Private Const SV_TYPE_SERVER_UNIX As Long = SV_TYPE_XENIX_SERVER
Private Const SV_TYPE_NT As Long = &H1000
Private Const SV_TYPE_WFW As Long = &H2000
Private Const SV_TYPE_SERVER_MFPN As Long = &H4000
Private Const SV_TYPE_SERVER_NT As Long = &H8000
Private Const SV_TYPE_POTENTIAL_BROWSER As Long = &H10000
Private Const SV_TYPE_BACKUP_BROWSER As Long = &H20000
Private Const SV_TYPE_MASTER_BROWSER As Long = &H40000
Private Const SV_TYPE_DOMAIN_MASTER As Long = &H80000
Private Const SV_TYPE_SERVER_OSF As Long = &H100000
Private Const SV_TYPE_SERVER_VMS As Long = &H200000
Private Const SV_TYPE_WINDOWS As Long = &H400000
'Windows95 +
Private Const SV_TYPE_DFS As Long = &H800000
'Root of a DFS tree
Private Const SV_TYPE_CLUSTER_NT As Long = &H1000000 'NT
Cluster
Private Const SV_TYPE_TERMINALSERVER As Long = &H2000000
'Terminal Server
Private Const SV_TYPE_DCE As Long = &H10000000 'IBM
DSS
Private Const SV_TYPE_ALTERNATE_XPORT As Long = &H20000000
'return alternate transport
Private Const SV_TYPE_LOCAL_LIST_ONLY As Long = &H40000000
'return local only
Private Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000
Private Const SV_TYPE_ALL As Long = &HFFFFFFFF
Private Const MAJOR_VERSION_MASK As Long = &HF

Private Structure SERVER_INFO_101
Dim PlatformID As Integer
Dim Name As IntPtr
Dim VersionMajor As Integer
Dim VersionMinor As Integer
Dim Type As Integer
Dim Comment As IntPtr
End Structure

Private Declare Auto Function NetApiBufferFree Lib "netapi32.dll"
(ByVal bufptr As IntPtr) As Integer
Private Declare Auto Function NetServerEnum Lib "netapi32.dll" _
(ByVal nullptr As Integer, _
ByVal Level As Integer, _
ByRef BufPtr As IntPtr, _
ByVal BufMaxLen As Integer, _
ByRef EntriesRead As Integer,
_
ByRef TotalEntries As Integer,
_
ByVal ServerType As Integer, _
ByVal Domain As IntPtr, _
ByVal ResumeHandle As Integer)
As Integer
Public Enum MACHINEPLATFORM As Integer
DOS = 300
OS2 = 400
NT_2K_XP = 500
OSF = 600
VMS = 700
End Enum

Public Enum MACHINETYPE As Integer
Workstation = &H1
Server = &H2
SQLServer = &H4
DomainController = &H8
BackupDomainController = &H10
TimeSource = &H20
AppleFileProtocolServer = &H40
NovellServer = &H80
DomainMember = &H100
PrintServer = &H200
DialinServer = &H400
UnixOrXenix = &H800
NT_2K_XP = &H1000
WinForWorkgroups = &H2000
ServerFilePrintForNetware = &H4000
ServerNT = &H8000
PotentialBrowser = &H10000
BackupBrowser = &H20000
MasterBrowser = &H40000
DomainMaster = &H80000
ServerOSF = &H100000
ServerVMS = &H200000
Windows95OrHigher = &H400000
DFSRoot = &H800000
ClusterNode = &H1000000
TerminalServer = &H2000000
ClusterVirtualServer = &H4000000
End Enum

Public Structure MACHINE
Dim Platform As MACHINEPLATFORM
Dim Name As String
Dim VersionMajor As Integer
Dim VersionMinor As Integer
Dim TypeBits As MACHINETYPE
Dim Comment As String
End Structure

Public Shared Function GetMasterBrowseList() As MACHINE()
Return GetMasterBrowseList(Nothing)
End Function

Public Shared Function GetMasterBrowseList(ByVal Domain As String)
As MACHINE()
Dim BrowseList As New ArrayList

Dim BufPtr As IntPtr
Dim se101 As SERVER_INFO_101
Dim StructSize As Integer = Marshal.SizeOf(se101)

Dim EntriesRead As Integer
Dim TotalEntries As Integer
Dim ServerType As Integer = SV_TYPE_WORKSTATION Or
SV_TYPE_SERVER
Dim DomainPtr As IntPtr

If Domain Is Nothing Or Domain = "" Then
DomainPtr = New IntPtr(0)
Else
'we are faking a constant string here with a BSTR by
appending
'a null character. we'll free it afterwards
DomainPtr = Marshal.StringToBSTR(Domain & Chr(0))
End If

Dim ret As Integer = NetServerEnum(0, _
101, _
BufPtr, _
MAX_PREFERRED_LENGTH, _
EntriesRead, _
TotalEntries, _
ServerType, _
DomainPtr, _
0)
Marshal.FreeBSTR(DomainPtr)

Try
If ret = NERR_SUCCESS Then
Dim nextbufptr As IntPtr

For t As Integer = 0 To EntriesRead - 1
'copy memory chunk OF SIZE StructSize
' FROM LOC bufptr +
(StructSize + t)
' INTO LOC se101
nextbufptr = New IntPtr(BufPtr.ToInt32 +
(StructSize * t))
se101 = Marshal.PtrToStructure(nextbufptr,
GetType(SERVER_INFO_101))

Dim themachine As MACHINE
themachine.Name =
Marshal.PtrToStringUni(se101.Name)
themachine.Comment =
Marshal.PtrToStringUni(se101.Comment)
themachine.Platform = se101.PlatformID
themachine.TypeBits = se101.Type
themachine.VersionMajor = se101.VersionMajor And
MAJOR_VERSION_MASK
themachine.VersionMinor = se101.VersionMinor

BrowseList.Add(themachine)
Next
End If
Catch ex As Exception
Throw ex
Finally
NetApiBufferFree(BufPtr)
End Try

Return BrowseList.ToArray(GetType(MACHINE))

End Function

End Class
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

This will only work if you are connected to an active directory. Add
a reference to system.directoryservices. Replace NetworkName with your
network name.

Dim de As New System.DirectoryServices.DirectoryEntry("LDAP://NetworkName")

Dim ds As New System.DirectoryServices.DirectorySearcher(de)

Dim r As System.DirectoryServices.SearchResult

ds.Filter = "(objectClass=computer)"

Try

For Each r In ds.FindAll

Dim s As String

Console.WriteLine(r.GetDirectoryEntry.Name.ToStrin g)

Next

Catch e As Exception

Console.WriteLine(e.ToString)

End Try
This NetServerEnum api will work for a network without a domain. I
pasted the code for a sample console app below.

Imports System.Runtime.InteropServices

Module Module1

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 Unicode Function NetServerEnum Lib "Netapi32.dll" _

(ByVal Servername As Integer, ByVal level As Integer, _

ByRef buffer As Integer, ByVal PrefMaxLen As Integer, _

ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, _

ByVal ServerType As Integer, ByVal DomainName As String, _

ByRef ResumeHandle As Integer) As Integer

Declare Function NetApiBufferFree Lib "Netapi32.dll" _

(ByVal lpBuffer As Integer) As Integer

Private Const SV_TYPE_SERVER As Integer = &H2 ' All Servers

Sub Main()

Dim ComputerInfo As Computer_info_101

Dim i, MaxLenPref, level, ret, EntriesRead, TotalEntries, ResumeHandle As
Integer

Dim BufPtr As Integer

Dim iPtr As IntPtr

MaxLenPref = -1

level = 101

ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries,
_

SV_TYPE_SERVER, "MSHOME", ResumeHandle) ' Replace MSHOME with your workgroup
name

If ret <> 0 Then

Console.WriteLine("An Error has occured")

Return

End If

' loop thru the entries

For i = 0 To EntriesRead - 1

' copy the stuff into our structure

Dim ptr As IntPtr = New IntPtr(BufPtr)

computerInfo = CType(Marshal.PtrToStructure(ptr,
GetType(Computer_info_101)), _

Computer_info_101)

BufPtr = BufPtr + Len(ComputerInfo)

Console.WriteLine(computerInfo.Name)

Next

NetApiBufferFree(BufPtr)

Console.Write("Press Enter to End")

Dim s As String = Console.ReadLine()

End Sub

End Module

Ken
---------------

"Joe User" <ja*********@yahoo.com> wrote in message
news:a2**************************@posting.google.c om...
Hi - I found the class below on another website (www.devx.com)
and can't seem to figure out how to make a call to this class to
retrieve the machines and put them in a listbox.

I am definitely a beginner in vb programming, and would really
appreciate the help.

TIA!

Imports System.Runtime.InteropServices

Public Class MasterBrowseList

Private Const MAX_PREFERRED_LENGTH As Integer = -1&
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
Private Const SV_TYPE_WORKSTATION As Long = &H1
Private Const SV_TYPE_SERVER As Long = &H2
Private Const SV_TYPE_SQLSERVER As Long = &H4
Private Const SV_TYPE_DOMAIN_CTRL As Long = &H8
Private Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10
Private Const SV_TYPE_TIME_SOURCE As Long = &H20
Private Const SV_TYPE_AFP As Long = &H40
Private Const SV_TYPE_NOVELL As Long = &H80
Private Const SV_TYPE_DOMAIN_MEMBER As Long = &H100
Private Const SV_TYPE_PRINTQ_SERVER As Long = &H200
Private Const SV_TYPE_DIALIN_SERVER As Long = &H400
Private Const SV_TYPE_XENIX_SERVER As Long = &H800
Private Const SV_TYPE_SERVER_UNIX As Long = SV_TYPE_XENIX_SERVER
Private Const SV_TYPE_NT As Long = &H1000
Private Const SV_TYPE_WFW As Long = &H2000
Private Const SV_TYPE_SERVER_MFPN As Long = &H4000
Private Const SV_TYPE_SERVER_NT As Long = &H8000
Private Const SV_TYPE_POTENTIAL_BROWSER As Long = &H10000
Private Const SV_TYPE_BACKUP_BROWSER As Long = &H20000
Private Const SV_TYPE_MASTER_BROWSER As Long = &H40000
Private Const SV_TYPE_DOMAIN_MASTER As Long = &H80000
Private Const SV_TYPE_SERVER_OSF As Long = &H100000
Private Const SV_TYPE_SERVER_VMS As Long = &H200000
Private Const SV_TYPE_WINDOWS As Long = &H400000
'Windows95 +
Private Const SV_TYPE_DFS As Long = &H800000
'Root of a DFS tree
Private Const SV_TYPE_CLUSTER_NT As Long = &H1000000 'NT
Cluster
Private Const SV_TYPE_TERMINALSERVER As Long = &H2000000
'Terminal Server
Private Const SV_TYPE_DCE As Long = &H10000000 'IBM
DSS
Private Const SV_TYPE_ALTERNATE_XPORT As Long = &H20000000
'return alternate transport
Private Const SV_TYPE_LOCAL_LIST_ONLY As Long = &H40000000
'return local only
Private Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000
Private Const SV_TYPE_ALL As Long = &HFFFFFFFF
Private Const MAJOR_VERSION_MASK As Long = &HF

Private Structure SERVER_INFO_101
Dim PlatformID As Integer
Dim Name As IntPtr
Dim VersionMajor As Integer
Dim VersionMinor As Integer
Dim Type As Integer
Dim Comment As IntPtr
End Structure

Private Declare Auto Function NetApiBufferFree Lib "netapi32.dll"
(ByVal bufptr As IntPtr) As Integer
Private Declare Auto Function NetServerEnum Lib "netapi32.dll" _
(ByVal nullptr As Integer, _
ByVal Level As Integer, _
ByRef BufPtr As IntPtr, _
ByVal BufMaxLen As Integer, _
ByRef EntriesRead As Integer,
_
ByRef TotalEntries As Integer,
_
ByVal ServerType As Integer, _
ByVal Domain As IntPtr, _
ByVal ResumeHandle As Integer)
As Integer
Public Enum MACHINEPLATFORM As Integer
DOS = 300
OS2 = 400
NT_2K_XP = 500
OSF = 600
VMS = 700
End Enum

Public Enum MACHINETYPE As Integer
Workstation = &H1
Server = &H2
SQLServer = &H4
DomainController = &H8
BackupDomainController = &H10
TimeSource = &H20
AppleFileProtocolServer = &H40
NovellServer = &H80
DomainMember = &H100
PrintServer = &H200
DialinServer = &H400
UnixOrXenix = &H800
NT_2K_XP = &H1000
WinForWorkgroups = &H2000
ServerFilePrintForNetware = &H4000
ServerNT = &H8000
PotentialBrowser = &H10000
BackupBrowser = &H20000
MasterBrowser = &H40000
DomainMaster = &H80000
ServerOSF = &H100000
ServerVMS = &H200000
Windows95OrHigher = &H400000
DFSRoot = &H800000
ClusterNode = &H1000000
TerminalServer = &H2000000
ClusterVirtualServer = &H4000000
End Enum

Public Structure MACHINE
Dim Platform As MACHINEPLATFORM
Dim Name As String
Dim VersionMajor As Integer
Dim VersionMinor As Integer
Dim TypeBits As MACHINETYPE
Dim Comment As String
End Structure

Public Shared Function GetMasterBrowseList() As MACHINE()
Return GetMasterBrowseList(Nothing)
End Function

Public Shared Function GetMasterBrowseList(ByVal Domain As String)
As MACHINE()
Dim BrowseList As New ArrayList

Dim BufPtr As IntPtr
Dim se101 As SERVER_INFO_101
Dim StructSize As Integer = Marshal.SizeOf(se101)

Dim EntriesRead As Integer
Dim TotalEntries As Integer
Dim ServerType As Integer = SV_TYPE_WORKSTATION Or
SV_TYPE_SERVER
Dim DomainPtr As IntPtr

If Domain Is Nothing Or Domain = "" Then
DomainPtr = New IntPtr(0)
Else
'we are faking a constant string here with a BSTR by
appending
'a null character. we'll free it afterwards
DomainPtr = Marshal.StringToBSTR(Domain & Chr(0))
End If

Dim ret As Integer = NetServerEnum(0, _
101, _
BufPtr, _
MAX_PREFERRED_LENGTH, _
EntriesRead, _
TotalEntries, _
ServerType, _
DomainPtr, _
0)
Marshal.FreeBSTR(DomainPtr)

Try
If ret = NERR_SUCCESS Then
Dim nextbufptr As IntPtr

For t As Integer = 0 To EntriesRead - 1
'copy memory chunk OF SIZE StructSize
' FROM LOC bufptr +
(StructSize + t)
' INTO LOC se101
nextbufptr = New IntPtr(BufPtr.ToInt32 +
(StructSize * t))
se101 = Marshal.PtrToStructure(nextbufptr,
GetType(SERVER_INFO_101))

Dim themachine As MACHINE
themachine.Name =
Marshal.PtrToStringUni(se101.Name)
themachine.Comment =
Marshal.PtrToStringUni(se101.Comment)
themachine.Platform = se101.PlatformID
themachine.TypeBits = se101.Type
themachine.VersionMajor = se101.VersionMajor And
MAJOR_VERSION_MASK
themachine.VersionMinor = se101.VersionMinor

BrowseList.Add(themachine)
Next
End If
Catch ex As Exception
Throw ex
Finally
NetApiBufferFree(BufPtr)
End Try

Return BrowseList.ToArray(GetType(MACHINE))

End Function

End Class
Nov 21 '05 #2

P: n/a
Thanks for the post Ken, but I'm not working in an AD environment
'yet'. Seems like it'd be easier if i was. :\

I'll take a look at the console code, but I was hoping someone could
show me how to use the code i posted, as I'd really like to know how
the function GetMasterBrowseList is to be used and what exactly it
returns.

Any ideas?
Thanks again!

joe

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message news:<ex**************@TK2MSFTNGP09.phx.gbl>...
Hi,

This will only work if you are connected to an active directory. Add
a reference to system.directoryservices. Replace NetworkName with your
network name.

Dim de As New System.DirectoryServices.DirectoryEntry("LDAP://NetworkName")

Dim ds As New System.DirectoryServices.DirectorySearcher(de)

Dim r As System.DirectoryServices.SearchResult

ds.Filter = "(objectClass=computer)"

Try

For Each r In ds.FindAll

Dim s As String

Console.WriteLine(r.GetDirectoryEntry.Name.ToStrin g)

Next

Catch e As Exception

Console.WriteLine(e.ToString)

End Try
This NetServerEnum api will work for a network without a domain. I
pasted the code for a sample console app below.

Imports System.Runtime.InteropServices

Module Module1

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 Unicode Function NetServerEnum Lib "Netapi32.dll" _

(ByVal Servername As Integer, ByVal level As Integer, _

ByRef buffer As Integer, ByVal PrefMaxLen As Integer, _

ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, _

ByVal ServerType As Integer, ByVal DomainName As String, _

ByRef ResumeHandle As Integer) As Integer

Declare Function NetApiBufferFree Lib "Netapi32.dll" _

(ByVal lpBuffer As Integer) As Integer

Private Const SV_TYPE_SERVER As Integer = &H2 ' All Servers

Sub Main()

Dim ComputerInfo As Computer_info_101

Dim i, MaxLenPref, level, ret, EntriesRead, TotalEntries, ResumeHandle As
Integer

Dim BufPtr As Integer

Dim iPtr As IntPtr

MaxLenPref = -1

level = 101

ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries,
_

SV_TYPE_SERVER, "MSHOME", ResumeHandle) ' Replace MSHOME with your workgroup
name

If ret <> 0 Then

Console.WriteLine("An Error has occured")

Return

End If

' loop thru the entries

For i = 0 To EntriesRead - 1

' copy the stuff into our structure

Dim ptr As IntPtr = New IntPtr(BufPtr)

computerInfo = CType(Marshal.PtrToStructure(ptr,
GetType(Computer_info_101)), _

Computer_info_101)

BufPtr = BufPtr + Len(ComputerInfo)

Console.WriteLine(computerInfo.Name)

Next

NetApiBufferFree(BufPtr)

Console.Write("Press Enter to End")

Dim s As String = Console.ReadLine()

End Sub

End Module

Ken
---------------

"Joe User" <ja*********@yahoo.com> wrote in message
news:a2**************************@posting.google.c om...
Hi - I found the class below on another website (www.devx.com)
and can't seem to figure out how to make a call to this class to
retrieve the machines and put them in a listbox.

I am definitely a beginner in vb programming, and would really
appreciate the help.

TIA!

Imports System.Runtime.InteropServices

Public Class MasterBrowseList

Private Const MAX_PREFERRED_LENGTH As Integer = -1&
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
Private Const SV_TYPE_WORKSTATION As Long = &H1
Private Const SV_TYPE_SERVER As Long = &H2
Private Const SV_TYPE_SQLSERVER As Long = &H4
Private Const SV_TYPE_DOMAIN_CTRL As Long = &H8
Private Const SV_TYPE_DOMAIN_BAKCTRL As Long = &H10
Private Const SV_TYPE_TIME_SOURCE As Long = &H20
Private Const SV_TYPE_AFP As Long = &H40
Private Const SV_TYPE_NOVELL As Long = &H80
Private Const SV_TYPE_DOMAIN_MEMBER As Long = &H100
Private Const SV_TYPE_PRINTQ_SERVER As Long = &H200
Private Const SV_TYPE_DIALIN_SERVER As Long = &H400
Private Const SV_TYPE_XENIX_SERVER As Long = &H800
Private Const SV_TYPE_SERVER_UNIX As Long = SV_TYPE_XENIX_SERVER
Private Const SV_TYPE_NT As Long = &H1000
Private Const SV_TYPE_WFW As Long = &H2000
Private Const SV_TYPE_SERVER_MFPN As Long = &H4000
Private Const SV_TYPE_SERVER_NT As Long = &H8000
Private Const SV_TYPE_POTENTIAL_BROWSER As Long = &H10000
Private Const SV_TYPE_BACKUP_BROWSER As Long = &H20000
Private Const SV_TYPE_MASTER_BROWSER As Long = &H40000
Private Const SV_TYPE_DOMAIN_MASTER As Long = &H80000
Private Const SV_TYPE_SERVER_OSF As Long = &H100000
Private Const SV_TYPE_SERVER_VMS As Long = &H200000
Private Const SV_TYPE_WINDOWS As Long = &H400000
'Windows95 +
Private Const SV_TYPE_DFS As Long = &H800000
'Root of a DFS tree
Private Const SV_TYPE_CLUSTER_NT As Long = &H1000000 'NT
Cluster
Private Const SV_TYPE_TERMINALSERVER As Long = &H2000000
'Terminal Server
Private Const SV_TYPE_DCE As Long = &H10000000 'IBM
DSS
Private Const SV_TYPE_ALTERNATE_XPORT As Long = &H20000000
'return alternate transport
Private Const SV_TYPE_LOCAL_LIST_ONLY As Long = &H40000000
'return local only
Private Const SV_TYPE_DOMAIN_ENUM As Long = &H80000000
Private Const SV_TYPE_ALL As Long = &HFFFFFFFF
Private Const MAJOR_VERSION_MASK As Long = &HF

Private Structure SERVER_INFO_101
Dim PlatformID As Integer
Dim Name As IntPtr
Dim VersionMajor As Integer
Dim VersionMinor As Integer
Dim Type As Integer
Dim Comment As IntPtr
End Structure

Private Declare Auto Function NetApiBufferFree Lib "netapi32.dll"
(ByVal bufptr As IntPtr) As Integer
Private Declare Auto Function NetServerEnum Lib "netapi32.dll" _
(ByVal nullptr As Integer, _
ByVal Level As Integer, _
ByRef BufPtr As IntPtr, _
ByVal BufMaxLen As Integer, _
ByRef EntriesRead As Integer,
_
ByRef TotalEntries As Integer,
_
ByVal ServerType As Integer, _
ByVal Domain As IntPtr, _
ByVal ResumeHandle As Integer)
As Integer
Public Enum MACHINEPLATFORM As Integer
DOS = 300
OS2 = 400
NT_2K_XP = 500
OSF = 600
VMS = 700
End Enum

Public Enum MACHINETYPE As Integer
Workstation = &H1
Server = &H2
SQLServer = &H4
DomainController = &H8
BackupDomainController = &H10
TimeSource = &H20
AppleFileProtocolServer = &H40
NovellServer = &H80
DomainMember = &H100
PrintServer = &H200
DialinServer = &H400
UnixOrXenix = &H800
NT_2K_XP = &H1000
WinForWorkgroups = &H2000
ServerFilePrintForNetware = &H4000
ServerNT = &H8000
PotentialBrowser = &H10000
BackupBrowser = &H20000
MasterBrowser = &H40000
DomainMaster = &H80000
ServerOSF = &H100000
ServerVMS = &H200000
Windows95OrHigher = &H400000
DFSRoot = &H800000
ClusterNode = &H1000000
TerminalServer = &H2000000
ClusterVirtualServer = &H4000000
End Enum

Public Structure MACHINE
Dim Platform As MACHINEPLATFORM
Dim Name As String
Dim VersionMajor As Integer
Dim VersionMinor As Integer
Dim TypeBits As MACHINETYPE
Dim Comment As String
End Structure

Public Shared Function GetMasterBrowseList() As MACHINE()
Return GetMasterBrowseList(Nothing)
End Function

Public Shared Function GetMasterBrowseList(ByVal Domain As String)
As MACHINE()
Dim BrowseList As New ArrayList

Dim BufPtr As IntPtr
Dim se101 As SERVER_INFO_101
Dim StructSize As Integer = Marshal.SizeOf(se101)

Dim EntriesRead As Integer
Dim TotalEntries As Integer
Dim ServerType As Integer = SV_TYPE_WORKSTATION Or
SV_TYPE_SERVER
Dim DomainPtr As IntPtr

If Domain Is Nothing Or Domain = "" Then
DomainPtr = New IntPtr(0)
Else
'we are faking a constant string here with a BSTR by
appending
'a null character. we'll free it afterwards
DomainPtr = Marshal.StringToBSTR(Domain & Chr(0))
End If

Dim ret As Integer = NetServerEnum(0, _
101, _
BufPtr, _
MAX_PREFERRED_LENGTH, _
EntriesRead, _
TotalEntries, _
ServerType, _
DomainPtr, _
0)
Marshal.FreeBSTR(DomainPtr)

Try
If ret = NERR_SUCCESS Then
Dim nextbufptr As IntPtr

For t As Integer = 0 To EntriesRead - 1
'copy memory chunk OF SIZE StructSize
' FROM LOC bufptr +
(StructSize + t)
' INTO LOC se101
nextbufptr = New IntPtr(BufPtr.ToInt32 +
(StructSize * t))
se101 = Marshal.PtrToStructure(nextbufptr,
GetType(SERVER_INFO_101))

Dim themachine As MACHINE
themachine.Name =
Marshal.PtrToStringUni(se101.Name)
themachine.Comment =
Marshal.PtrToStringUni(se101.Comment)
themachine.Platform = se101.PlatformID
themachine.TypeBits = se101.Type
themachine.VersionMajor = se101.VersionMajor And
MAJOR_VERSION_MASK
themachine.VersionMinor = se101.VersionMinor

BrowseList.Add(themachine)
Next
End If
Catch ex As Exception
Throw ex
Finally
NetApiBufferFree(BufPtr)
End Try

Return BrowseList.ToArray(GetType(MACHINE))

End Function

End Class

Nov 21 '05 #3

P: n/a
Try this:

dim oMasterBrowseList as New MasterBrowseList.GetMasterBrowseList()
dim oMachine as MasterBrowseList.MACHINE

For Each oMachine in oMasterBrowseList
List1.Additem oMachine.Name
Next
From http://developmentnow.com/g/38_2004_...in-network.htm

Posted via DevelopmentNow.com Groups
http://www.developmentnow.com
Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.