How can I get the names of all computers in my local network?

  • Thread starter Thread starter Stanimir Angeloff
  • Start date Start date
S

Stanimir Angeloff

Hello everybody,

How can I get the names of all computers in my local network? I tried
this code (microsoft.public.dotnet.general - Listing neighbor
computers):

DirectorySearcher src = new
DirectorySearcher("(objectClass=computer)");
foreach(SearchResult res in src.FindAll())
{
Console.WriteLine(res.Path);
}

But I get the following error:

The specified domain either does not exist or could not be contacted.

Thank you in advance.
 
Hi,

This NetServerEnum api will work for a network without a domain. I
pasted the code for a sample console app below.

Imports System.Runtime.InteropServices

Module Module1

Structure Computer_info_101

Public Platform_ID As Integer

<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Name As String

Public Version_Major As Integer

Public Version_Minor As Integer

Public Type As Integer

<MarshalAsAttribute(UnmanagedType.LPWStr)> Public Comment As String

End Structure

Declare Unicode Function NetServerEnum Lib "Netapi32.dll" _

(ByVal Servername As Integer, ByVal level As Integer, _

ByRef buffer As Integer, ByVal PrefMaxLen As Integer, _

ByRef EntriesRead As Integer, ByRef TotalEntries As Integer, _

ByVal ServerType As Integer, ByVal DomainName As String, _

ByRef ResumeHandle As Integer) As Integer

Declare Function NetApiBufferFree Lib "Netapi32.dll" _

(ByVal lpBuffer As Integer) As Integer

Private Const SV_TYPE_SERVER As Integer = &H2 ' All Servers

Sub Main()

Dim ComputerInfo As Computer_info_101

Dim i, MaxLenPref, level, ret, EntriesRead, TotalEntries, ResumeHandle As
Integer

Dim BufPtr As Integer

Dim iPtr As IntPtr

MaxLenPref = -1

level = 101

ret = NetServerEnum(0, level, BufPtr, MaxLenPref, EntriesRead, TotalEntries,
_

SV_TYPE_SERVER, "MSHOME", ResumeHandle) ' Replace MSHOME with your workgroup
name

If ret <> 0 Then

Console.WriteLine("An Error has occured")

Return

End If



' loop thru the entries

For i = 0 To EntriesRead - 1

' copy the stuff into our structure

Dim ptr As IntPtr = New IntPtr(BufPtr)

computerInfo = CType(Marshal.PtrToStructure(ptr,
GetType(Computer_info_101)), _

Computer_info_101)

BufPtr = BufPtr + Len(ComputerInfo)

Console.WriteLine(computerInfo.Name)

Next

NetApiBufferFree(BufPtr)

Console.Write("Press Enter to End")

Dim s As String = Console.ReadLine()

End Sub





End Module

Ken
 
Hello Ken Tucker,



Thank you for your reply.



The code works fine, it listed all computers in MSHOME workgroup, but my
local network contains many other workgroups (Artcd, Stadium, Olympiad…). I
would like to enumerate all users, no matter in which workgroup they belong.
Is this possible? Is the “unsafe” code the only way?



Thank you again ;)
 
Back
Top