473,698 Members | 2,932 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Director yServices 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 2161
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_CONNEC TED As Short = &H1S

Private Const RESOURCE_GLOBAL NET As Short = &H2S

Private Const RESOURCE_REMEMB ERED As Short = &H3S

Private Const RESOURCETYPE_AN Y As Short = &H0S

Private Const RESOURCETYPE_DI SK As Short = &H1S

Private Const RESOURCETYPE_PR INT As Short = &H2S

Private Const RESOURCETYPE_UN KNOWN As Short = &HFFFFS

Private Const RESOURCEUSAGE_C ONNECTABLE As Short = &H1S

Private Const RESOURCEUSAGE_C ONTAINER As Short = &H2S

Private Const RESOURCEUSAGE_R ESERVED As Integer = &H80000000

Private Const RESOURCEDISPLAY TYPE_GENERIC As Short = &H0S

Private Const RESOURCEDISPLAY TYPE_DOMAIN As Short = &H1S

Private Const RESOURCEDISPLAY TYPE_SERVER As Short = &H2S

Private Const RESOURCEDISPLAY TYPE_SHARE As Short = &H3S

Private Const RESOURCEDISPLAY TYPE_FILE As Short = &H4S

Private Const RESOURCEDISPLAY TYPE_GROUP As Short = &H5S

Private Const RESOURCEDISPLAY TYPE_NETWORK As Short = &H6S

Private Const RESOURCEDISPLAY TYPE_ROOT As Short = &H7S

Private Const RESOURCEDISPLAY TYPE_ADMINSHARE As Short = &H8S

Private Const RESOURCEDISPLAY TYPE_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.d ll" _

(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 NetApiBufferFre e Lib "Netapi32.d ll" _

(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 WNetGetLastErro r Lib "mpr.dll" Alias
"WNetGetLastErr orA" _

(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 "WNetOpenEn umA" _

(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 WNetEnumResourc e Lib "mpr.dll" Alias
"WNetEnumResour ceA" _

(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_strNetworkNam e As String

Sub Main()

Dim lngEnumHandle As Integer

Dim nrInit As NETRESOURCE

lngEnumHandle = GetNetworkHandl e(nrInit, True)

If lngEnumHandle = 0 Then

Console.WriteLi ne("No network.")

Exit Sub

End If

RecurseNetworkL evels(lngEnumHa ndle)

WNetCloseEnum(l ngEnumHandle)

End Sub
Private Sub RecurseNetworkL evels(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(GPT R, cbBuff)

lngReturn = WNetEnumResourc e(lngEnumHandle , cCount, lpBuff, cbBuff)

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

'ERROR_MORE_DAT A()

If lpBuff <> 0 Then GlobalFree(lpBu ff)

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

End If

Loop While lngReturn = 234

If lngReturn = 0 Then

p = lpBuff

ReDim nrNetInfo(cCoun t - 1)

For i = 1 To cCount

Dim ptrUser As New IntPtr(p)

stNetRes = Marshal.PtrToSt ructure(ptrUser , GetType(NETRESO URCE))

nrNetInfo(i - 1) = stNetRes

p = p + Len(stNetRes)

Next

Else

Dim Buffer As String = Space(255)

FormatMessage(F ORMAT_MESSAGE_F ROM_SYSTEM, 0, lngReturn, _

LANG_NEUTRAL, Buffer, 255, 0)

Debug.WriteLine (Buffer.Trim)

If lpBuff <> 0 Then GlobalFree(lpBu ff)

Exit Sub

End If

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

If cCount <= 0 Then Exit Sub

For i = 0 To cCount - 1

ProcessEntry(nr NetInfo(i))

If RESOURCEUSAGE_C ONTAINER = (nrNetInfo(i).d wUsage And _

RESOURCEUSAGE_C ONTAINER) Then

lngEnumHandle = GetNetworkHandl e(nrNetInfo(i))

If lngEnumHandle <> 0 Then

RecurseNetworkL evels(lngEnumHa ndle)

WNetCloseEnum(l ngEnumHandle)

End If

End If

Next

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

If lpBuff <> 0 Then GlobalFree(lpBu ff)

End Sub

Private Sub ProcessEntry(By Ref nr As NETRESOURCE)

Dim strName, strComment As String

Dim ptrString As New IntPtr(nr.lpRem oteName)

strName = Marshal.PtrToSt ringAnsi(ptrStr ing)

ptrString = New IntPtr(nr.lpCom ment)

strComment = Marshal.PtrToSt ringAnsi(ptrStr ing)

Select Case nr.dwDisplayTyp e

Case RESOURCEDISPLAY TYPE_DOMAIN

m_strNetworkNam e = strName

Console.Write(" Network Name ")

Console.WriteLi ne(m_strNetwork Name)

Case RESOURCEDISPLAY TYPE_SERVER

Console.Write(" Server ")

Console.Write(s trName)

Console.Write(" ")

Console.WriteLi ne(strComment)

Case RESOURCEDISPLAY TYPE_SHARE

Console.Write(" Share ")

Console.Write(s trName)

Console.Write(" ")

Console.WriteLi ne(strComment)

End Select

End Sub

Private Function GetNetworkHandl e(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(RE SOURCE_GLOBALNE T, RESOURCETYPE_DI SK, _

0, Nothing, lngEnumHandle)

Else

lngReturn = WNetOpenEnum(RE SOURCE_GLOBALNE T, RESOURCETYPE_DI SK, _

0, nrInit, lngEnumHandle)

End If

If lngReturn = 0 Then

Return lngEnumHandle

Else

Dim strName As String

Dim ptrString As New IntPtr(nrInit.l pRemoteName)

strName = Marshal.PtrToSt ringAnsi(ptrStr ing)

Console.WriteLi ne(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.WriteLi ne("An extended error has occurred. ERROR_EXTENDED_ ERROR ")

WNetGetLastErro r(lngReturn, a, 255, b, 255)

Console.WriteLi ne(a.Trim)

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

Case Else

Dim Buffer As New String(" ", 255)

Buffer = Space(255)

FormatMessage(F ORMAT_MESSAGE_F ROM_SYSTEM, 0, lngReturn, LANG_NEUTRAL,
Buffer, 255, 0)

Console.WriteLi ne(Trim(Buffer) )

End Select

Return 0

End If

End Function

End Module
Ken
-----------------
"Thomas Müller" <sa******@hotma il.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.Director yServices 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
1758
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 out the name of the instances so that I could create a list of them e.g.
7
1488
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 this list to find the best player at defending, attacking, midfield and goalkeeping. However there is more than one defender per team so I therefore need it to find the next best player.
4
2179
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 value(s) for the key being indexed, but received 1 value(s). I have tried the following: Dim Names(1) as Object
1
2526
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 for a particular shared folder. Thanks.
4
1907
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 retrieving this type of info. TIA! Lee
19
4442
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 array 4) Figure out if the original number is evenly divisible by any of the numbers in the array.
6
4743
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 predicate for the <set>. Here is an example: #include <string> #include <set> using namespace std;
2
1856
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 provide this, if not, coudl you provide me some guiadance with unmanaged APIs or wrappers. -- Fernik http://www.fernik.com.ar
275
12285
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
8611
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.