CodeItBetter Programming Another VB Programming Blog

How to Display networked computers in a list box

Posted on January 4, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
'Network - How to Display networked computers in a list box
Option Explicit
 
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
    'PURPOSE: DISPLAYS NETWORK NAME AND ALL COMPUTERS ON THE NETWORK IN A LIST BOX
    'PARAMETER: ListBox (or any control with similar interface, such as ComboBox) 
    'in which to display list of computers
    'RETURNS: True if successful, false otherwise

    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
    'test to see if list is a list box type control
    list.AddItem " "
    list.Clear
    If Err.Number > 0 Then Exit Function
 
    On Error GoTo ErrorHandler
 
    'Setup the NETRESOURCE input structure.
    NR.lpRemoteName = 0
    cbBuff = 10000
    cCount = &HFFFFFFFF
 
    'Open a Net enumeration operation handle: hEnum.
    res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, NR, hEnum)
 
    If res = 0 Then
        'Create a buffer large enough for the results.
        '10000 bytes should be sufficient.
        lpBuff = GlobalAlloc(GPTR, cbBuff)
        'Call the enumeration function.
        res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
        If res = 0 Then
            p = lpBuff
            'WNetEnumResource fills the buffer with an array of
            'NETRESOURCE structures. Walk through the list and print each local and remote name.
            For i = 1 To cCount
                ' All we get back are the Global Network Containers --- Enumerate each of these
                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)    'Close the enumeration
    End If
End Function
 
Private Function PointerToString(p As Long) As String
    'The values returned in the NETRESOURCE structures are pointers to
    'ANSI strings so they need to be converted to Visual Basic Strings.
    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
 
    'Setup the NETRESOURCE input structure.
    cbBuff = 10000
    cCount = &HFFFFFFFF
 
    'Open a Net enumeration operation handle: hEnum.
    res = WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, NR, hEnum)
 
    If res = 0 Then
        'Create a buffer large enough for the results.
        '10000 bytes should be sufficient.
        lpBuff = GlobalAlloc(GPTR, cbBuff)
        'Call the enumeration function.
        res = WNetEnumResource(hEnum, cCount, lpBuff, cbBuff)
 
        If res = 0 Then
            p = lpBuff
            'WNetEnumResource fills the buffer with an array of
            'NETRESOURCE structures. Walk through the list and print
            'each remote name.
            For i = 1 To cCount
                CopyMemory NR, ByVal p, LenB(NR)
                list.AddItem "Network Computer #" & i & " " & PointerToString(NR.lpRemoteName)
                p = p + LenB(NR)
            Next i
        End If
 
        If lpBuff <> 0 Then GlobalFree (lpBuff)
        WNetCloseEnum (hEnum)    'Close the enumeration
    End If
End Sub
Filed under: Network Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.