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
' WNetEnumResourc e function.
<DllImport("mpr .dll")_
Public Shared Function WNetOpenEnum(By Val dwScope As ResourceScope, ByVal
dwType As ResourceType, ByVal dwUsage As ResourceUsage, ByVal lpNetResource
As NETRESOURCE, ByRef lphEnum As IntPtr) As Integer
End Function
' The WNetEnumResourc e function continues an enumeration of network
resources
' that was started by a call to the WNetOpenEnum function.
<DllImport("mpr .dll")_
Public Shared Function WNetEnumResourc e(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(B yVal hEnum As IntPtr) As Integer
End Function
' Enum ResourceScope.
Public Enum ResourceScope
RESOURCE_CONNEC TED = 1
RESOURCE_GLOBAL NET
RESOURCE_REMEMB ERED
End Enum
' Enum ResourceType.
Public Enum ResourceType
RESOURCETYPE_AN Y = 0
RESOURCETYPE_DI SK
RESOURCETYPE_PR INT
End Enum
' Enum ResourceDisplay Type.
Public Enum ResourceDisplay Type
RESOURCEDISPLAY TYPE_GENERIC = 0
RESOURCEDISPLAY TYPE_DOMAIN
RESOURCEDISPLAY TYPE_SERVER
RESOURCEDISPLAY TYPE_SHARE
End Enum
' Enum ResourceUsage.
Public Enum ResourceUsage
RESOURCEUSAGE_A LL = 0
RESOURCEUSAGE_C ONNECTABLE
RESOURCEUSAGE_C ONTAINER
End Enum
' Enum Error Codes.
Public Const NO_ERROR As Integer = 0
Public Const ERROR_NO_MORE_I TEMS As Integer = 259
' Structure NETRESOURCE.
<StructLayout(L ayoutKind.Seque ntial)_
Public Class NETRESOURCE
Public dwScope As ResourceScope = 0
Public dwType As ResourceType = 0
Public dwDisplayType As ResourceDisplay Type = 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 EnumerateServer s(ByVal rScope As ResourceScope, ByVal rType As
ResourceType, ByVal rDisplayType As ResourceDisplay Type, 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.AllocHG lobal(CInt(bSiz e))
' Allocate memory.
Dim eEntries As UInteger = 1
' Enumerate all possible entries
' Start an enumeration of network resources or existing connections.
rAPI = WNetOpenEnum(rS cope, 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 = WNetEnumResourc e(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.PtrToSt ructure(buffer, pNR)
' Add lpRemoteName from NETRESOURCE to Array list.
aList.Add(pNR.l pRemoteName)
' If the NETRESOURCE structure represents a container resource,
' call the EnumerateServer s function recursively.
If (ResourceUsage. RESOURCEUSAGE_C ONTAINER = (pNR.dwUsage And
ResourceUsage.R ESOURCEUSAGE_CO NTAINER)) Then
EnumerateServer s(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(h Enum)
End If
' Frees memory previously allocated from the unmanaged memory of the
' process with AllocHGlobal.
Marshal.FreeHGl obal(DirectCast (buffer, IntPtr))
End Sub
Private aList As New ArrayList()
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
Button2.Enabled = False
Dim nResource As New NETRESOURCE()
EnumerateServer s(ResourceScope .RESOURCE_GLOBA LNET,
ResourceType.RE SOURCETYPE_DISK ,
ResourceDisplay Type.RESOURCEDI SPLAYTYPE_GENER IC,
ResourceUsage.R ESOURCEUSAGE_CO NTAINER, 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.GetHostEntr y(s.Substring(2 , s.Length - 2))
Dim IpA() As IPAddress = ipE.AddressList
For i As Integer = 0 To IpA.GetUpperBou nd(0)
' get the ip adresses
Debug.WriteLine (IpA(i).ToStrin g)
Next
End If
Next
' Cleanup.
aList.Clear()
Button2.Enabled = True
End Sub
End Class
"kimiraikko nen" <ki************ *@gmail.comschr eef in bericht
news:11******** **************@ d55g2000hsg.goo glegroups.com.. .
Hello,
I've been working on a project and i wonder how to get list of the LAN
computer names in a tree list which are involved under the same
workgroup name?
What can be the code be?
Very thanks.