Finding names of nertwork computers

  • Thread starter Thread starter =?iso-8859-1?Q?Thomas_M=FCller?=
  • Start date Start date
?

=?iso-8859-1?Q?Thomas_M=FCller?=

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
 
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
-----------------
 
Thank you for your help, this was exactly what I was
looking for.

MfG Thomas
 
Back
Top