PDA

Click to See Complete Forum and Search --> : Finding computers


Mats_Högberg
Dec 2nd, 1999, 02:01 PM
I'd like to know if it's possible to enumerate all computers (names) that are connected to your LAN?!? It ought to be possible, since you can use "Find -> Computer" from the Start menu to perform this task, but I'd like to be able to use it in a VB app. All samples on networking presumes that you know the name of the computer you'd like to connect to beforehand. Reason for my curiosity is that I'm developing a WinPopup like "intercom" app, and would like to put all available recipients in a dropdown...

vbsquare
Dec 2nd, 1999, 09:41 PM
I know that this is not a complete solution, but it will enumerate the Win NT workstations and servers:

Public Const SV_TYPE_WORKSTATION = &H3
Global i As Integer
Global F As Integer
Global BWArray(512) As Byte
Global SPtr As Long
Global TotalEntries As Long
Global EntriesRead As Long
Global ResumeHandle As Long
Global BufLen As Long
Global DNArray() As Byte
Global SNArray() As Byte

Type MungeLong
X As Long
Dummy As Integer
End Type
Type MungeInt
XLo As Integer
XHi As Integer
Dummy As Integer
End Type

Declare Function NetServerEnum100 Lib “netapi32.dll” Alias “NetServerEnum”
(ServerName As Byte, ByVal Level As Long, Buffer As Long, ByVal PrefMaxLen
As Long, EntriesRead As Long, TotalEntries As Long, ByVal ServerType As
Long, DomainName As Byte, ResumeHandle As Long) As Long
Public Function EnumStations(ByVal SName As String, ByVal DName As String)
As Long
Dim TempPtr As MungeLong, TempStr As MungeInt
SNArray = SName & vbNullChar
DNArray = DName & vbNullChar
BufLen = 512
ResumeHandle = 0
Result = NetServerEnum100(SNArray(0), 100, SPtr, BufLen, EntriesRead,_
TotalEntries, SV_TYPE_WORKSTATION, DNArray(0), ResumeHandle)
EnumStations = Result
Call Message
F = 0
i = 1
Do
For i = 1 To EntriesRead
Result = PtrToInt(TempStr.XLo, SPtr + (i - 1) * 4, 2)
Result = PtrToInt(TempStr.XHi, SPtr + (i - 1) * 4 + 2, 2)
LSet TempPtr = TempStr
Result = PtrToStr(BWArray(0), TempPtr.X)
If Result <> 0 Then
For F = 0 To Forms.Count - 1
Forms(F).lstStations.AddItem BWArray
Next F
End If
Next i
Loop Until EntriesRead = TotalEntries
Result = NetAPIBufferFree(SPtr)
End Function The following code should help you:
Option Explicit

' Constants for type of computer
Private Const SV_TYPE_WORKSTATION = &H1&
Private Const SV_TYPE_SERVER = &H2&
Private Const SV_TYPE_DOMAIN_CTRL = &H8&
Private Const SV_TYPE_NT = &H1000&
Private Const SV_TYPE_SERVER_NT = &H8000&
Private Const SV_TYPE_WINDOWS = &H400000
Private Const SV_TYPE_ALL = &HFFFF

' Type used by NetServerEnum
Private Type SERVER_INFO_101
wki101_platform_id As Long
wki101_servername As Long
wki101_langroup As Long
wki101_ver_major As Long
wki101_ver_minor As Long
wki101_lanroot As Long
End Type

' API declarations
Private Declare Function NetServerEnum Lib "Netapi32" _
(ByVal sServerName As String, _
ByVal lLevel As Long, _
vBuffer As Long, _
lPreferedMaxLen As Long, _
lEntriesRead As Long, _
lTotalEntries As Long, _
ByVal vServerType As Long, _
ByVal sDomain As String, _
vResume As Long) As Long
Private Declare Function NetApiBufferFree _
Lib "Netapi32" _
(ByVal lBuffer&) As Long
Private Declare Sub lstrcpyW _
Lib "kernel32" _
(vDest As Any, ByVal vSrc As Any)
Private Declare Sub RtlMoveMemory _
Lib "kernel32" _
(dest As Any, vSrc As Any, ByVal lSize&)
Private Declare Function GetComputerName _
Lib "kernel32" Alias "GetComputerNameA" _
(ByVal lpBuffer As String, _
nSize As Long) As Long

Sub Main()
Dim vntServers As Variant
vntServers = GetServers()
End Sub

Public Function GetServers() As Variant
Dim pstrServerName As String
Dim pstrComputerName As String
Dim pstrDomainName As String
Dim pbytBuf(512) As Byte
Dim plngRtn As Long
Dim plngSrvInfoRtn As Long
Dim plngPreferedMaxLen As Long
Dim plngNumEntriesRead As Long
Dim plngTotalEntries As Long
Dim plngServerType As Long
Dim plngResume As Long
Dim plngCnt As Long
Dim lSeverInfo101StructPtr As Long
Dim plngServEnumLevel As Long
Dim plngPos As Long
Dim ptypServerInfo101 As SERVER_INFO_101
Dim plngLenComputerName As Long
Dim pvntComputers() As Variant

' Initialize the variables
pstrComputerName = vbNullString
pstrServerName = vbNullString
pstrDomainName = vbNullString
plngServEnumLevel = 101
plngServerType = SV_TYPE_NT

' Call NetServerEnum to get a list of Servers
plngRtn = NetServerEnum(pstrServerName, _
plngServEnumLevel, plngSrvInfoRtn, _
plngPreferedMaxLen, plngNumEntriesRead, _
plngTotalEntries, plngServerType, _
pstrDomainName, plngResume)

' NetServerEnum Index is 1 based -- iterate
' thru the return values to get the
' NT servers and workstations
plngCnt = 1
lSeverInfo101StructPtr = plngSrvInfoRtn

ReDim pvntComputers(1 To plngTotalEntries)

For plngCnt = 1 To plngTotalEntries
pstrComputerName = ""

RtlMoveMemory ptypServerInfo101, ByVal lSeverInfo101StructPtr, Len(ptypServerInfo101)

lstrcpyW pbytBuf(0), ptypServerInfo101.wki101_servername
On Error Resume Next
pstrComputerName = pbytBuf
plngPos = InStr(pstrComputerName, vbNullChar)
If plngPos > 0 Then
pstrComputerName = Mid$(pstrComputerName, 1, plngPos - 1)
End If

pvntComputers(plngCnt) = pstrComputerName
lSeverInfo101StructPtr = lSeverInfo101StructPtr + Len(ptypServerInfo101)

Next plngCnt

plngRtn = NetApiBufferFree(plngSrvInfoRtn)
GetServers = pvntComputers

End Function

------------------
"To the glory of God!"

Mats_Högberg
Dec 5th, 1999, 01:03 PM
Wow! Quite some code! I think I'll take a look at DirectPlay... :-)

/Mats