Get computers on network
'!!==========================
! PLACE THE FOLLOWING IN A FRM
'!!==========================
Option Explicit
'
Private Sub Form_Load()
Call GetComputers(Me.List1)
End Sub
'!!===========================
'!! PLACE THE FOLLOWING IN A MOD
'!!===========================
Option Explicit
Private Type SERVER_INFO_100
sv100_platform_id As Long
sv100_name As Long
End Type
Private Declare Function NetServerEnum Lib "netapi32" (ByVal servername As Long _
, ByVal level As Long, buf As Any, ByVal prefmaxlen As Long _
, entriesread As Long, totalentries As Long, ByVal servertype As Long _
, ByVal domain As Long, resume_handle As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any _
, uFrom As Any, ByVal lSize As Long)
Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal Buffer As Long) As Long
Private Declare Function lstrlenW Lib "kernel32" (ByVal lpString As Long) As Long
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const SV_TYPE_WORKSTATION As Long = &H1
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
'
Public Sub GetComputers(lst As ListBox)
'===============================================================================
Dim lngBufPtr As Long
Dim dwEntriesRead As Long
Dim dwTotalEntries As Long
Dim dwResumeHandle As Long
Dim se100 As SERVER_INFO_100
Dim lngSuccess As Long
Dim nStructSize As Long
Dim lngCounter As Long
Dim strComputer As String
Dim blnNoComputers As Boolean
nStructSize = LenB(se100)
'-- Call passing MAX_PREFERRED_LENGTH to have the API allocate required memory for the
' return values.
'-- The call is enumerating all machines on the network (SV_TYPE_ALL); however, by
' Or'ing specific bit masks for defined types you can customize the returned data.
' For example, a value of 0x00000003 combines the bit masks for
' SV_TYPE_WORKSTATION (0x00000001) and SV_TYPE_SERVER (0x00000002).
'-- dwServerName must be Null. The level parameter (100 here) specifies the data
' structure being used (in this case a SERVER_INFO_100 structure).
'-- The domain member is passed as Null, indicating machines on the primary
' domain are to be retrieved. If you decide to use this member, pass
' StrPtr("domain name"), not the string itself.
lngSuccess = NetServerEnum(0&, 100, lngBufPtr, MAX_PREFERRED_LENGTH, dwEntriesRead _
, dwTotalEntries, SV_TYPE_WORKSTATION, 0&, dwResumeHandle)
'-- If all goes well
If lngSuccess = NERR_SUCCESS And lngSuccess <> ERROR_MORE_DATA Then
lst.Clear
'-- Loop through the returned data, adding each machine to the list
For lngCounter = 0 To dwEntriesRead - 1
'-- Get one chunk of data and cast into an SERVER_INFO_100 struct
' in order to add the name to a list
CopyMemory se100, ByVal lngBufPtr + (nStructSize * lngCounter), nStructSize
strComputer = GetPointerToByteStringW(se100.sv100_name)
lst.AddItem strComputer
Next
blnNoComputers = (lst.ListCount = 0)
End If
'-- Clean up regardless of lngSuccess
Call NetApiBufferFree(lngBufPtr)
End Sub
Private Function GetPointerToByteStringW(ByVal dwData As Long) As String
'===============================================================================
Dim bytTemp() As Byte
Dim lngTempLen As Long
If dwData <> 0 Then
lngTempLen = lstrlenW(dwData) * 2
If lngTempLen <> 0 Then
ReDim bytTemp(0 To (lngTempLen - 1)) As Byte
CopyMemory bytTemp(0), ByVal dwData, lngTempLen
GetPointerToByteStringW = bytTemp
End If
End If
Erase bytTemp()
End Function
minhnhat2807 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.