okay, found a bunch of links on the web giving info on this... here's two pretty good ones:
Apparently, the process is slightly complex in that you have to import a bunch of functions and data structures from the dll named "netapi32" .
Using the code from these websites and the msdn documentation (
I wrote a module that creates two public functions. One returns a collection of all network servers on your network. The other returns all the shares on a particular server. If you use these functions you should be careful as collections only store 255 items (as far as I know) and the actual number of shares or servers may be much higher. Much of the code here was copied, but I felt it might be helpful if I posted it here also.
Code is included below.
-Venkman
Option Explicit
Private Type SERVER_INFO_101
sv101_platform_id As Long
sv101_name As Long
sv101_version_major As Long
sv101_version_minor As Long
sv101_type As Long
sv101_comment As Long
End Type
Public Type SHARE_INFO_1
Netname As String
ShareType As Long
Remark As String
End Type
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const ERROR_ACCESS_DENIED = 5&
Private Const NO_ERROR = 0
Private Const STYPE_DISKTREE = 0 ' /* disk share */
Private Const STYPE_PRINTQ = 1 ' /* printer share */
Private Const STYPE_DEVICE = 2
Private Const STYPE_IPC = 3
Private Const STYPE_SPECIAL = &H80000000
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(pTo As Any, uFrom As Any, _
ByVal lSize As Long)
Private Declare Function NetShareEnum Lib "netapi32" (ByVal lpServerName As Long, ByVal dwLevel As Long, lpBuffer As Any, ByVal dwPrefMaxLen As Long, EntriesRead As Long, TotalEntries As Long, hResume As Long) As Long
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 Function lstrlenW Lib "kernel32" _
(ByVal lpString As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal Buffer As Long) As Long
Private Function PointerToStringW(ByVal lpStringW As Long) As String
Dim Buffer() As Byte
Dim nLen As Long
If lpStringW Then
nLen = lstrlenW(lpStringW) * 2
If nLen Then
ReDim Buffer(0 To (nLen - 1)) As Byte
CopyMemory Buffer(0), ByVal lpStringW, nLen
PointerToStringW = Buffer
End If
End If
End Function
Private Function GetPointerToByteStringW(ByVal dwData As Long) As String
Dim tmp() As Byte
Dim tmplen As Long
If dwData <> 0 Then
tmplen = lstrlenW(dwData) * 2
If tmplen <> 0 Then
ReDim tmp(0 To (tmplen - 1)) As Byte
CopyMemory tmp(0), ByVal dwData, tmplen
GetPointerToByteStringW = tmp
End If
End If
End Function
Private Function PointerToDWord(ByVal lpDWord As Long) As Long
Call CopyMemory(PointerToDWord, ByVal lpDWord, 4)
End Function
Public Function GetServers() As Collection
Dim bufPtr As Long
Dim dwEntriesRead As Long
Dim dwTotalEntries As Long
Dim dwResumeHandle As Long
Dim lSuccess As Long
Const NERR_SUCCESS As Long = 0&
Const ERROR_MORE_DATA As Long = 234&
Dim cnt As Long
Dim se101 As SERVER_INFO_101
Dim nStructSize As Long
Dim colResult As New Collection
nStructSize = LenB(se101)
lSuccess = NetServerEnum(0&, 101, bufPtr, -1, dwEntriesRead, dwTotalEntries, &HFFFFFFFF, _
0&, dwResumeHandle)
If lSuccess = NERR_SUCCESS And _
lSuccess <> ERROR_MORE_DATA Then
For cnt = 0 To dwEntriesRead - 1
CopyMemory se101, ByVal bufPtr + (nStructSize * cnt), nStructSize
colResult.Add GetPointerToByteStringW(se101.sv101_name)
Next
End If
Call NetApiBufferFree(bufPtr)
Set GetServers = colResult
End Function
Public Function GetShares(strServer As String) As Collection
Const NO_ERROR = 0
Dim colRes As New Collection
Dim lpBuffer As Long
Dim EntriesRead As Long
Dim TotalEntries As Long
Dim hResume As Long
Dim Offset As Long
Dim nRet As Long
Dim i As Long
Dim Level As Integer
Dim currShareType As Long
Dim currName As String
Dim lpStrPtr As Long
Level = 1
lpStrPtr = StrPtr(strServer)
nRet = NetShareEnum(lpStrPtr, Level, lpBuffer, -1, EntriesRead, TotalEntries, hResume)
If nRet = NO_ERROR Then
If EntriesRead > 0 Then
For i = 0 To EntriesRead
currShareType = PointerToDWord(lpBuffer + Offset + 4)
If currShareType = 0 Then ' it's a disk
currName = PointerToStringW(PointerToDWord(lpBuffer + Offset))
If Right(currName, 1) <> "$" Then
colRes.Add currName
End If
End If
Offset = Offset + 12
Next
End If
Else
' handle the error
End If
Set GetShares = colRes
End Function