473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Finding names of nertwork computers

Hi,

I need a way to find the names of the computers that are
reachable over a network. Ideally when it is executed it
should report back wich computernames are present.

I have seen one solution with System.DirectoryServices but
that doesn't work for me because the required isn't
installed on the computer.

Any way to do this preferbly with existing methods would be
apreciated.

Thanks,
Thomas
Nov 20 '05 #1
2 2141
Hi,

You will have to use the api to get that info. Here is a sample
console app.

Imports System.Runtime.InteropServices

Module Module1

Private Const ERROR_SUCCESS = 0&

Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER As Short = &H100S

Private Const FORMAT_MESSAGE_FROM_SYSTEM As Short = &H1000S

Private Const LANG_NEUTRAL As Short = &H0S

Private Const SUBLANG_DEFAULT As Short = &H1S

Private Const GMEM_FIXED As Short = &H0S

Private Const GMEM_ZEROINIT As Short = &H40S

Private Const GPTR As Short = (GMEM_FIXED Or GMEM_ZEROINIT)

Private Const LEVEL_NETWORK As Short = 1

Private Const LEVEL_DOMAIN As Short = 2

Private Const LEVEL_SERVER As Short = 3

Private Const LEVEL_SHARE As Short = 4

Private Const LEVEL_DIRECTORY As Short = 5

Private Const LEVEL_FILE As Short = 6

Private Const RESOURCE_CONNECTED As Short = &H1S

Private Const RESOURCE_GLOBALNET As Short = &H2S

Private Const RESOURCE_REMEMBERED As Short = &H3S

Private Const RESOURCETYPE_ANY As Short = &H0S

Private Const RESOURCETYPE_DISK As Short = &H1S

Private Const RESOURCETYPE_PRINT As Short = &H2S

Private Const RESOURCETYPE_UNKNOWN As Short = &HFFFFS

Private Const RESOURCEUSAGE_CONNECTABLE As Short = &H1S

Private Const RESOURCEUSAGE_CONTAINER As Short = &H2S

Private Const RESOURCEUSAGE_RESERVED As Integer = &H80000000

Private Const RESOURCEDISPLAYTYPE_GENERIC As Short = &H0S

Private Const RESOURCEDISPLAYTYPE_DOMAIN As Short = &H1S

Private Const RESOURCEDISPLAYTYPE_SERVER As Short = &H2S

Private Const RESOURCEDISPLAYTYPE_SHARE As Short = &H3S

Private Const RESOURCEDISPLAYTYPE_FILE As Short = &H4S

Private Const RESOURCEDISPLAYTYPE_GROUP As Short = &H5S

Private Const RESOURCEDISPLAYTYPE_NETWORK As Short = &H6S

Private Const RESOURCEDISPLAYTYPE_ROOT As Short = &H7S

Private Const RESOURCEDISPLAYTYPE_ADMINSHARE As Short = &H8S

Private Const RESOURCEDISPLAYTYPE_DIRECTORY As Short = &H9S

Private Declare Function GetLastError Lib "kernel32" () As Integer

Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA"
_

(ByVal dwFlags As Integer, ByRef lpSource As Object, ByVal dwMessageId As
Integer, _

ByVal dwLanguageId As Integer, ByVal lpBuffer As String, ByVal nSize As
Integer, _

ByRef Arguments As Integer) As Integer

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 Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As
Integer, _

ByVal dwBytes As Integer) As Integer

Private Declare Function GlobalFree Lib "kernel32" (ByVal hMem As Integer)
As Integer

Private Declare Function WNetGetLastError Lib "mpr.dll" Alias
"WNetGetLastErrorA" _

(ByRef lpError As Integer, ByVal lpErrorBuf As String, _

ByVal nErrorBufSize As Integer, ByVal lpNameBuf As String, _

ByVal nNameBufSize As Integer) As Integer

Private 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

Private 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

Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As
Integer) As Integer

Private Structure NETRESOURCE

Dim dwScope As Integer

Dim dwType As Integer

Dim dwDisplayType As Integer

Dim dwUsage As Integer

Dim lpLocalName As Integer

Dim lpRemoteName As Integer

Dim lpComment As Integer

Dim lpProvider As Integer

End Structure

Private m_strNetworkName As String

Sub Main()

Dim lngEnumHandle As Integer

Dim nrInit As NETRESOURCE

lngEnumHandle = GetNetworkHandle(nrInit, True)

If lngEnumHandle = 0 Then

Console.WriteLine("No network.")

Exit Sub

End If

RecurseNetworkLevels(lngEnumHandle)

WNetCloseEnum(lngEnumHandle)

End Sub
Private Sub RecurseNetworkLevels(ByVal lngEnumHandle As Integer)

Dim nrNetInfo() As NETRESOURCE

Dim i As Integer

Dim s As String

Dim stNetRes As NETRESOURCE

Dim cbBuff As Integer = 1023 * Len(stNetRes) ' 32kb

Dim lpBuff As Integer

Dim cCount As Integer = -1 ' Retrieve All

Dim p As Integer ' Pointer

Dim lngReturn As Integer

Do

lpBuff = GlobalAlloc(GPTR, cbBuff)

lngReturn = WNetEnumResource(lngEnumHandle, cCount, lpBuff, cbBuff)

If lngReturn = 234 Then ' 234 More data is available.

'ERROR_MORE_DATA()

If lpBuff <> 0 Then GlobalFree(lpBuff)

cbBuff = cbBuff * 2 ' 64kb, 128kb ...

End If

Loop While lngReturn = 234

If lngReturn = 0 Then

p = lpBuff

ReDim nrNetInfo(cCount - 1)

For i = 1 To cCount

Dim ptrUser As New IntPtr(p)

stNetRes = Marshal.PtrToStructure(ptrUser, GetType(NETRESOURCE))

nrNetInfo(i - 1) = stNetRes

p = p + Len(stNetRes)

Next

Else

Dim Buffer As String = Space(255)

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, lngReturn, _

LANG_NEUTRAL, Buffer, 255, 0)

Debug.WriteLine(Buffer.Trim)

If lpBuff <> 0 Then GlobalFree(lpBuff)

Exit Sub

End If

'----------------------------------------------------------------

If cCount <= 0 Then Exit Sub

For i = 0 To cCount - 1

ProcessEntry(nrNetInfo(i))

If RESOURCEUSAGE_CONTAINER = (nrNetInfo(i).dwUsage And _

RESOURCEUSAGE_CONTAINER) Then

lngEnumHandle = GetNetworkHandle(nrNetInfo(i))

If lngEnumHandle <> 0 Then

RecurseNetworkLevels(lngEnumHandle)

WNetCloseEnum(lngEnumHandle)

End If

End If

Next

'---------------------------------------------------------------

If lpBuff <> 0 Then GlobalFree(lpBuff)

End Sub

Private Sub ProcessEntry(ByRef nr As NETRESOURCE)

Dim strName, strComment As String

Dim ptrString As New IntPtr(nr.lpRemoteName)

strName = Marshal.PtrToStringAnsi(ptrString)

ptrString = New IntPtr(nr.lpComment)

strComment = Marshal.PtrToStringAnsi(ptrString)

Select Case nr.dwDisplayType

Case RESOURCEDISPLAYTYPE_DOMAIN

m_strNetworkName = strName

Console.Write("Network Name ")

Console.WriteLine(m_strNetworkName)

Case RESOURCEDISPLAYTYPE_SERVER

Console.Write("Server ")

Console.Write(strName)

Console.Write(" ")

Console.WriteLine(strComment)

Case RESOURCEDISPLAYTYPE_SHARE

Console.Write(" Share ")

Console.Write(strName)

Console.Write(" ")

Console.WriteLine(strComment)

End Select

End Sub

Private Function GetNetworkHandle(ByRef nrInit As NETRESOURCE, _

Optional ByRef fInit As Boolean = False) As Integer

Dim lngReturn As Integer

Dim lngEnumHandle As Integer

If fInit Then

lngReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, _

0, Nothing, lngEnumHandle)

Else

lngReturn = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, _

0, nrInit, lngEnumHandle)

End If

If lngReturn = 0 Then

Return lngEnumHandle

Else

Dim strName As String

Dim ptrString As New IntPtr(nrInit.lpRemoteName)

strName = Marshal.PtrToStringAnsi(ptrString)

Console.WriteLine(strName & " - " & lngReturn)

Select Case lngReturn

Case 1208

Dim a As New String(" ", 255)

Dim b As New String(" ", 255)

Dim Buffer As New String(" ", 255)

Console.WriteLine("An extended error has occurred. ERROR_EXTENDED_ERROR ")

WNetGetLastError(lngReturn, a, 255, b, 255)

Console.WriteLine(a.Trim)

If b.Trim.Length > 0 Then Debug.WriteLine(b.Trim)

Case Else

Dim Buffer As New String(" ", 255)

Buffer = Space(255)

FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, lngReturn, LANG_NEUTRAL,
Buffer, 255, 0)

Console.WriteLine(Trim(Buffer))

End Select

Return 0

End If

End Function

End Module
Ken
-----------------
"Thomas Müller" <sa******@hotmail.com> wrote in message
news:02****************************@phx.gbl...
Hi,

I need a way to find the names of the computers that are
reachable over a network. Ideally when it is executed it
should report back wich computernames are present.

I have seen one solution with System.DirectoryServices but
that doesn't work for me because the required isn't
installed on the computer.

Any way to do this preferbly with existing methods would be
apreciated.

Thanks,
Thomas

Nov 20 '05 #2
Thank you for your help, this was exactly what I was
looking for.

MfG Thomas
Nov 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: André | last post by:
Short version of what I am looking for: Given a class "public_class" which is instantiated a few times e.g. a = public_class() b = public_class() c = public_class() I would like to find...
7
by: Ben | last post by:
Hi In a list I have a number of soccer players. Each player has a different rating for attacking, defending, midfield fitness and goalkeeping. I have devised a while loop that goes through...
4
by: Charles A. Lackman | last post by:
Hello I have created a Dataview and have sorted it on the Date and ShiftNumber columns this works great, but when I want to use the Find Method it gives me the following error: Expecting 2...
1
by: Keith Smith | last post by:
My software will be installed on peer-to-peer networks and sometimes 2000/2003 domain networks. How can I get a list of the computers on the network? My goal is to search each of these computers...
4
by: lgbjr | last post by:
Hi All, I need to retrieve a list of computer names that are available on a network and a list of databases that are available in MSDE2K. Can someone point me in the right direction for...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
6
by: Martin | last post by:
Hi I need to maintain a <setof pointers to objects, and it must be sorted based on the values of pointed objects, not pointer values. I can achieve this easily by defining my own comparing...
2
by: =?Utf-8?B?RmVybmlr?= | last post by:
Hi, I need to get a list of the computers in my WorkGroup LAN, with its names in a similar way you get the available Drives from a PC with DriveInfo.GetDrives(). Is there a managed class that...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.