how to get list of LAN computers in workgroup

  • Thread starter Thread starter kimiraikkonen
  • Start date Start date
K

kimiraikkonen

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.
 
Hi,

You can try this one.

http://www.vb-tips.com/dbpages.aspx?Search=users

Cor

"kimiraikkonen" <[email protected]> schreef in bericht





- Show quoted text -

Hello,
Thanks for quick and helpful reply.
I looked at that link what you've sent:
http://www.vb-tips.com/ADListComputers.aspx


I've dragged ComboBox and pasted code into necessary area, but i got
following errors:

1-Type 'DirectoryEntry' is not defined
2-Type 'DirectorySearcher' is not defined
3-Type 'SearchResult' is not defined

I'm using VB.NET 2005 Express which is free version. Are these errors
because of these? Doesn't free version of Vb.NET 2005 support that
functions?

Thank you.
 
kimiraikkonen said:
Hello,
Thanks for quick and helpful reply.
I looked at that link what you've sent:
http://www.vb-tips.com/ADListComputers.aspx


I've dragged ComboBox and pasted code into necessary area, but i got
following errors:

1-Type 'DirectoryEntry' is not defined
2-Type 'DirectorySearcher' is not defined
3-Type 'SearchResult' is not defined

I'm using VB.NET 2005 Express which is free version. Are these errors
because of these? Doesn't free version of Vb.NET 2005 support that
functions?

Thank you.

You probably missed this line "You need to add a reference to
system.directoryservices and an import"

Also it proabbaly isn`t going to work cause AFAIK active directory requires
a domain


Regards

Michel
 
Newbie Coder,

What kind of spam you are sending all the time. You are talking about bugs,
however have never told which one.

We here are no politican you know, if your site is so good, then you get
visitors enough, if not, don't tell lies.

There can be bugs in the code we give, although it is mostly completely
tested.

Cor
 
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
 
Back
Top