Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Network shares list 1

Status
Not open for further replies.

btaber

Programmer
Joined
May 26, 2002
Messages
307
Location
US
What is the easiest wat to get a list of shares on a network computer?
 
WMI could get the computer, but didn;t work for the shares... found another way though in case anyone is wondering:

Code:
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.Marshal

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi)> _
    Public Structure NETRESOURCE
        Dim dwScope As Integer
        Dim dwType As Integer
        Dim dwDisplayType As Integer
        Dim dwUsage As Integer
        Dim lpLocalName As String
        Dim lpRemoteName As String
        Dim lpComment As String
        Dim lpProvider As String
    End Structure

    Declare Function WNetOpenEnum Lib "mpr.dll" Alias "WNetOpenEnumA" (ByVal dwScope As Integer, ByVal dwType As Integer, ByVal dwUsage As Integer, ByRef lpNetResource As NETRESOURCE, ByRef lphEnum As Integer) As Integer
    Declare Function WNetEnumResource Lib "mpr.dll" Alias "WNetEnumResourceA" (ByVal hEnum As Integer, ByRef lpcCount As Integer, ByVal lpBuffer As Integer, ByRef lpBufferSize As Integer) As Integer
    Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Integer) As Integer

    Private Const RESOURCEDISPLAYTYPE_DOMAIN As Integer = &H1
    Private Const RESOURCEDISPLAYTYPE_NETWORK As Integer = &H6
    Private Const RESOURCE_GLOBALNET As Integer = &H2
    Private Const RESOURCEUSAGE_CONTAINER As Integer = &H2
    Private Const RESOURCETYPE_ANY As Integer = &H0
    Private Const ERROR_NO_MORE_ITEMS As Integer = 259&


    Sub GetEnunNet(ByRef Net As Object, ByVal LevelLimit As Integer, ByVal StartDom As String)
        Dim TempNet As NETRESOURCE = Nothing
        Dim NetHandle, ret, BufSize, NetCount, i, Buf As Integer
        Dim iPtr, iBuf As IntPtr
        Dim Resource, RType As String
        Static Level As Integer
        Static Provider, Domain, PCName As String
        'Dim dr As DataRow


        ' Is this the first time thru?
        If IsNothing(Net) Then
            'MyForm.RowCount = 0
            Level = 0
            Provider = ""
            Domain = ""
            PCName = ""
            ' Did we specify a starting Domain? If so, then build a NETRESOURCE()
            ' structure by hand as the starting point
            If StartDom <> "" Then
                Provider = "Microsoft Windows Network"
                Domain = StartDom
                ' start at the PC level
                Level = 2
                TempNet.dwDisplayType = RESOURCEDISPLAYTYPE_DOMAIN
                TempNet.dwScope = RESOURCE_GLOBALNET
                TempNet.dwType = RESOURCETYPE_ANY
                TempNet.dwUsage = RESOURCEUSAGE_CONTAINER
                TempNet.lpProvider = "Microsoft Windows Network"
                TempNet.lpRemoteName = StartDom
                ret = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, TempNet, NetHandle)
            Else
                ret = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, RESOURCEUSAGE_CONTAINER, Nothing, NetHandle)
            End If
        Else
            ret = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, Net, NetHandle)
        End If

        ' open the WNet handle
        If ret <> 0 Then
            Debug.WriteLine("Yikes, Something went wrong at WNetOpenEnum,Error=" & ret)
            Debug.WriteLine("Level=" & Level & ", Provider=" & Provider & ",Domain=" & Domain & ", PCName=" & PCName)
            Exit Sub
        End If

        ' Create 16k buffer to handle the results
        BufSize = 16384
        iBuf = AllocHGlobal(BufSize)

        Do
            ' Query the handle returned by WNetOpenEnum
            Buf = iBuf.ToInt32
            NetCount = -1
            ret = WNetEnumResource(NetHandle, NetCount, Buf, BufSize)
            If ret > 0 And ret <> ERROR_NO_MORE_ITEMS Then
                Debug.WriteLine("Yikes, Something went wrong atWNetEnumResource, Error=" & ret)
                Debug.WriteLine("Level=" & Level & ", Provider=" & Provider & ", Domain=" & Domain & ", PCName=" & PCName)
                ' silently ignore these errors
                FreeHGlobal(iBuf)
                WNetCloseEnum(NetHandle)
                Exit Sub
            End If

            ' loop thru the answers
            For i = 1 To NetCount
                ' some wizardry to "walk" the array of NETRESOURCEstructures()
                iPtr = New IntPtr(Buf)
                TempNet = CType(Marshal.PtrToStructure(iPtr, GetType(NETRESOURCE)), NETRESOURCE)
                Buf = Buf + Len(TempNet)

                Select Case Level
                    Case 0
                        ' "level 0", means the provider
                        Provider = TempNet.lpRemoteName
                        Domain = ""
                        PCName = ""
                        Resource = ""
                        RType = ""
                        Console.WriteLine("Provider: " & Provider)
                        Application.DoEvents()
                    Case 1
                        ' "level 1" means the domain/workgroup name
                        Domain = TempNet.lpRemoteName
                        PCName = ""
                        Resource = ""
                        RType = ""
                        Console.WriteLine("Domain: " & Domain)
                        Application.DoEvents()
                    Case 2
                        ' "level 2", means the PC name
                        PCName = TempNet.lpRemoteName
                        Resource = ""
                        RType = ""
                        Console.WriteLine("PCName: " & PCName)
                        Application.DoEvents()
                    Case 3
                        ' "Level 3" means the file/print shares
                        Resource = TempNet.lpRemoteName
                        Console.WriteLine("Resource: " & Resource)
                        Application.DoEvents()
                        Select Case TempNet.dwType
                            Case 1
                                RType = "Disk"
                            Case 2
                                RType = "Print"
                            Case Else
                                RType = CStr(TempNet.dwType)
                        End Select
                End Select

                'make a recursive call to this subroutine
                If Level < LevelLimit Then
                    ' increment the level by 1
                    Level += 1
                    GetEnunNet(TempNet, LevelLimit, StartDom)
                    Level -= 1
                End If
            Next
        Loop While ret <> ERROR_NO_MORE_ITEMS

        ' clean up and go home
        FreeHGlobal(iBuf)
        WNetCloseEnum(NetHandle)
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top