Imports System.Runtime.InteropServices
Imports System.Net
Public Class Form1
' The WNetOpenEnum function starts an enumeration of network resources or
' existing connections. You can continue the enumeration by calling the
' WNetEnumResource function.
<DllImport("mpr.dll")> _
Public Shared Function WNetOpenEnum(ByVal dwScope As ResourceScope, ByVal
dwType As ResourceType, ByVal dwUsage As ResourceUsage, ByVal lpNetResource
As NETRESOURCE, ByRef lphEnum As IntPtr) As Integer
End Function
' The WNetEnumResource function continues an enumeration of network
resources
' that was started by a call to the WNetOpenEnum function.
<DllImport("mpr.dll")> _
Public Shared Function WNetEnumResource(ByVal hEnum As IntPtr, ByRef
lpcCount As UInteger, ByVal lpBuffer As IntPtr, ByRef lpBufferSize As
UInteger) As Integer
End Function
' The WNetCloseEnum function ends a network resource enumeration started by
a
' call to the WNetOpenEnum function.
<DllImport("mpr.dll")> _
Public Shared Function WNetCloseEnum(ByVal hEnum As IntPtr) As Integer
End Function
' Enum ResourceScope.
Public Enum ResourceScope
RESOURCE_CONNECTED = 1
RESOURCE_GLOBALNET
RESOURCE_REMEMBERED
End Enum
' Enum ResourceType.
Public Enum ResourceType
RESOURCETYPE_ANY = 0
RESOURCETYPE_DISK
RESOURCETYPE_PRINT
End Enum
' Enum ResourceDisplayType.
Public Enum ResourceDisplayType
RESOURCEDISPLAYTYPE_GENERIC = 0
RESOURCEDISPLAYTYPE_DOMAIN
RESOURCEDISPLAYTYPE_SERVER
RESOURCEDISPLAYTYPE_SHARE
End Enum
' Enum ResourceUsage.
Public Enum ResourceUsage
RESOURCEUSAGE_ALL = 0
RESOURCEUSAGE_CONNECTABLE
RESOURCEUSAGE_CONTAINER
End Enum
' Enum Error Codes.
Public Const NO_ERROR As Integer = 0
Public Const ERROR_NO_MORE_ITEMS As Integer = 259
' Structure NETRESOURCE.
<StructLayout(LayoutKind.Sequential)> _
Public Class NETRESOURCE
Public dwScope As ResourceScope = 0
Public dwType As ResourceType = 0
Public dwDisplayType As ResourceDisplayType = 0
Public dwUsage As ResourceUsage = 0
Public lpLocalName As String = Nothing
Public lpRemoteName As String = Nothing
Public lpComment As String = Nothing
Public lpProvider As String = Nothing
End Class
Public Sub EnumerateServers(ByVal rScope As ResourceScope, ByVal rType As
ResourceType, ByVal rDisplayType As ResourceDisplayType, ByVal rUsage As
ResourceUsage, ByVal pNR As NETRESOURCE)
' Variables.
Dim rAPI As Integer
' API return.
Dim hEnum As IntPtr = IntPtr.Zero
' Handle to the enum.
Dim bSize As UInteger = 16384
' 16K is a good size.
Dim buffer As IntPtr = Marshal.AllocHGlobal(CInt(bSize))
' Allocate memory.
Dim eEntries As UInteger = 1
' Enumerate all possible entries
' Start an enumeration of network resources or existing connections.
rAPI = WNetOpenEnum(rScope, rType, rUsage, pNR, hEnum)
' Process errors with an application-defined error handler.
If rAPI <> NO_ERROR Then
Else
Do
' Continue an enumeration of network resources.
rAPI = WNetEnumResource(hEnum, eEntries, buffer, bSize)
If rAPI <> NO_ERROR Then
' Process errors with an application-defined error handler.
Return
Else
' Marshal data from an unmanaged block of memory to a
' managed object.
Marshal.PtrToStructure(buffer, pNR)
' Add lpRemoteName from NETRESOURCE to Array list.
aList.Add(pNR.lpRemoteName)
' If the NETRESOURCE structure represents a container resource,
' call the EnumerateServers function recursively.
If (ResourceUsage.RESOURCEUSAGE_CONTAINER = (pNR.dwUsage And
ResourceUsage.RESOURCEUSAGE_CONTAINER)) Then
EnumerateServers(rScope, rType, rDisplayType, rUsage, pNR)
End If
' Loop until rAPI != 259, Constant = no more items.
End If
Loop While rAPI <> ERROR_NO_MORE_ITEMS
' Call WNetCloseEnum to end the enumeration.
WNetCloseEnum(hEnum)
End If
' Frees memory previously allocated from the unmanaged memory of the
' process with AllocHGlobal.
Marshal.FreeHGlobal(DirectCast(buffer, IntPtr))
End Sub
Private aList As New ArrayList()
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Button2.Enabled = False
Dim nResource As New NETRESOURCE()
EnumerateServers(ResourceScope.RESOURCE_GLOBALNET,
ResourceType.RESOURCETYPE_DISK,
ResourceDisplayType.RESOURCEDISPLAYTYPE_GENERIC,
ResourceUsage.RESOURCEUSAGE_CONTAINER, nResource)
For Each s As String In aList
If s.Substring(0, 2) = "\\" Then
Debug.WriteLine(s)
' get the asociated ip adresses for this resource
Dim ipE As IPHostEntry = Dns.GetHostEntry(s.Substring(2, s.Length - 2))
Dim IpA() As IPAddress = ipE.AddressList
For i As Integer = 0 To IpA.GetUpperBound(0)
' get the ip adresses
Debug.WriteLine(IpA(i).ToString)
Next
End If
Next
' Cleanup.
aList.Clear()
Button2.Enabled = True
End Sub
End Class