Link to home
Start Free TrialLog in
Avatar of davidshields
davidshields

asked on

Enumerate My Shares

Is there any way in VB that I can find out what folders I am sharing in VB on Win98 platform?

I know the machine knows what I'm sharing as explorer highlights them with the little hand, but I cannot for the life of me see a way of programmatically getting a list of what shares I have and what folders they refer to.

I know its easier on NT / 2000, but remember, it MUST work on Win98 SE.

Showing shares on something else seems easier, but knowing my own machine - bah !

David
Avatar of twalgrave
twalgrave

Here's some code that may help:

Private Type SHARE_INFO_2
  Netname As String
  ShareType As Long
  Remark As String
  Permissions As Long
  MaxUsers As Long
  CurrentUsers As Long
  Path As String
  Password As String
End Type

Private Const MAX_PREFERRED_LENGTH As Long = -1
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 NetApiBufferFree Lib "netapi32" (ByVal lpBuffer 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 lstrlenW Lib "kernel32" (ByVal PointerToString As Long) As Long

Private Const NO_ERROR = 0
Private Const ERROR_ACCESS_DENIED = 5&

Private Sub Command1_Click()
***************replace YOURSERVERNAMEHERE************
EnumSharesNT "YOURSERVERNAMEHERE"
End Sub

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 PointerToDWord(ByVal lpDWord As Long) As Long
  Call CopyMemory(PointerToDWord, ByVal lpDWord, 4)
End Function

Private Function EnumSharesNT(ByVal Server As String) As Long
  Dim Level As Long
  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 shi() As SHARE_INFO_2
 
  ' convert Server to null pointer if none requested.
  ' this has the effect of asking for the local machine.
  If Len(Server) = 0 Then Server = vbNullString

  ' ask for all available shares; try level 2 first
  Level = 2
  nRet = NetShareEnum(StrPtr(Server), Level, lpBuffer, MAX_PREFERRED_LENGTH, EntriesRead, TotalEntries, hResume)
 
  If nRet = ERROR_ACCESS_DENIED Then
     ' bummer -- need admin privs for level 2, drop to level 1
     Level = 1
     nRet = NetShareEnum(StrPtr(Server), Level, lpBuffer, MAX_PREFERRED_LENGTH, EntriesRead, TotalEntries, hResume)
  End If
 
  If nRet = NO_ERROR Then
     ' make sure there are shares to decipher
     If EntriesRead > 0 Then
        ' prepare UDT buffer to hold all share info
        ReDim shi(0 To EntriesRead - 1)
        ' loop through API buffer, extracting each element
        For i = 0 To EntriesRead - 1
           With shi(i)
              .Netname = PointerToStringW(PointerToDWord(lpBuffer + Offset))
              .ShareType = PointerToDWord(lpBuffer + Offset + 4)
              .Remark = PointerToStringW(PointerToDWord(lpBuffer + Offset + 8))
              If Level = 2 Then
                 .Permissions = PointerToDWord(lpBuffer + Offset + 12)
                 .MaxUsers = PointerToDWord(lpBuffer + Offset + 16)
                 .CurrentUsers = PointerToDWord(lpBuffer + Offset + 20)
                 .Path = PointerToStringW(PointerToDWord(lpBuffer + Offset + 24))
                 .Password = PointerToStringW(PointerToDWord(lpBuffer + Offset + 28))
                 Offset = Offset + Len(shi(i))
              Else
                 Offset = Offset + 12  ' Len(SHARE_INFO_1)
              End If
   Debug.Print .Netname & " - " & .Path
           End With
        Next i
     End If
     
     ' return number of entries found
     If Level = 1 Then
        ' negative if we don't have admin privs
        EnumSharesNT = -EntriesRead
     ElseIf Level = 2 Then
        EnumSharesNT = EntriesRead
     End If
  End If
 
  ' clean up
  If lpBuffer Then
     Call NetApiBufferFree(lpBuffer)
  End If
End Function
I don't know if this works on 98 though.
I have no Win98 platform to test, can this code working?
    shell "Command.com /c net share > c:\ShareLst.txt"
Avatar of davidshields

ASKER

twalgrave: doesnt seem to work on 98. Used your example, and got:

Run-time error '453'
Can't find DLL entry point NetShareEnum in netapi32

so looks like this function is not in the 98 dll.

TigerShao - I really want to avoid shelling out to command processor.

Thanks guys. Looks like I'm on my own here !
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in Community Support that this question is:
- refund and close
Please leave any comments here within the
next seven days.
ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial