Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What users are on the network? 1

Status
Not open for further replies.

aliinal

Technical User
Jun 26, 2001
104
TR
How can i learn the computer names on a network?

It must be possible but how?

Thanks...
 
Hi,

Here is the module for it.
Enjoy

Option Explicit

Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As Long
lpRemoteName As Long
lpComment As Long
lpProvider As Long
End Type

Private Declare Function WNetOpenEnum Lib "mpr.dll" Alias _
"WNetOpenEnumA" (ByVal dwScope As Long, ByVal dwType As Long, _
ByVal dwUsage As Long, lpNetResource As Any, lphEnum As Long) As Long

Private Declare Function WNetEnumResource Lib "mpr.dll" Alias _
"WNetEnumResourceA" (ByVal hEnum As Long, lpcCount As Long, _
ByVal lpBuffer As Long, lpBufferSize As Long) As Long

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

Private Const RESOURCE_CONNECTED = &H1
Private Const RESOURCE_GLOBALNET = &H2
Private Const RESOURCE_REMEMBERED = &H3

Private Const RESOURCETYPE_ANY = &H0
Private Const RESOURCETYPE_DISK = &H1
Private Const RESOURCETYPE_PRINT = &H2
Private Const RESOURCETYPE_UNKNOWN = &HFFFF

Private Const RESOURCEUSAGE_CONNECTABLE = &H1
Private Const RESOURCEUSAGE_CONTAINER = &H2
Private Const RESOURCEUSAGE_RESERVED = &H80000000

Private Const GMEM_FIXED = &H0
Private Const GMEM_ZEROINIT = &H40
Private Const GPTR = (GMEM_FIXED Or GMEM_ZEROINIT)

Private Declare Function GlobalAlloc Lib "kernel32" _
(ByVal wFlags As Long, ByVal dwBytes As Long) As Long

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

Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (hpvDest As Any, hpvSource As Any, _
ByVal cbCopy As Long)

Private Declare Function CopyPointer2String Lib _
"kernel32" Alias "lstrcpyA" (ByVal NewString As _
String, ByVal OldString As Long) As Long

Public Function DoNetEnum(list As Object) As Boolean
Dim hEnum As Long, lpBuff As Long, NR As NETRESOURCE
Dim cbBuff As Long, cCount As Long
Dim p As Long, res As Long, I As Long

On Error Resume Next
list.AddItem " "
list.Clear
If Err.Number > 0 Then Exit Function

On Error GoTo ErrorHandler

NR.lpRemoteName = 0
cbBuff = 10000
cCount = &HFFFFFFFF

res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, NR, hEnum)

If res = 0 Then
lpBuff = GlobalAlloc(GPTR, cbBuff)
res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
If res = 0 Then
p = lpBuff
For I = 1 To cCount
CopyMemory NR, ByVal p, LenB(NR)
'list.AddItem "Network Name " & PointerToString(NR.lpRemoteName)
DoNetEnum2 NR, list
p = p + LenB(NR)
Next I
End If
DoNetEnum = True

ErrorHandler:
On Error Resume Next
If lpBuff <> 0 Then GlobalFree (lpBuff)
WNetCloseEnum (hEnum)
End If
End Function

Private Function PointerToString(p As Long) As String
Dim s As String
s = String(255, Chr$(0))
CopyPointer2String s, p
PointerToString = Left(s, InStr(s, Chr$(0)) - 1)
End Function

Private Sub DoNetEnum2(NR As NETRESOURCE, list As Object)
Dim hEnum As Long, lpBuff As Long
Dim cbBuff As Long, cCount As Long
Dim p As Long, res As Long, I As Long

cbBuff = 10000
cCount = &HFFFFFFFF
res = WNetOpenEnum(RESOURCE_GLOBALNET, _
RESOURCETYPE_ANY, 0, NR, hEnum)

If res = 0 Then
lpBuff = GlobalAlloc(GPTR, cbBuff)
res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)

If res = 0 Then
p = lpBuff
For I = 1 To cCount
CopyMemory NR, ByVal p, LenB(NR)
'list.AddItem &quot;Network Computer #&quot; & i & &quot; &quot; & PointerToString(NR.lpRemoteName)
list.AddItem PointerToString(NR.lpRemoteName)
p = p + LenB(NR)
Next I
End If

If lpBuff <> 0 Then GlobalFree (lpBuff)
WNetCloseEnum (hEnum)
End If
End Sub


:)

bye

Mohan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top