CodeItBetter Programming Another VB Programming Blog

How to List Network Computers

Posted on January 5, 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
'System & API - How to List Network Computers
'Here is the code, that will get the list of Network Computers and other
'lists such as printers list, shares list etc., add it to your project.
'Add a listbox on the form and name it as  lstTerminals

Private Sub Form_Load()
    Dim SW As CNetworkEnum    'Decalre class
    Set SW = New CNetworkEnum    'Set class to SW
    Call SW.SetResourceType(0)    'Set resource type
    Call SW.Reset
    Dim ST As String
 
    ST = Replace(SW.GetServerList, "\\", "")    'Replace the \\ in the computer names

    'loop through the list of computer names and put the in the listbox
    For i = 1 To Len(ST)
        If Mid(ST, i, 1) = "," Then
            lstTerminals.AddItem (Left(ST, i - 1))
            ST = Right(ST, Len(ST) - i)
            i = 1
        End If
    Next i
End Sub
 
'And in a Class Module:

'VERSION 1.0 CLASS
'BEGIN
'  MultiUse = -1  'True
'  Persistable = 0  'NotPersistable
'  DataBindingBehavior = 0  'vbNone
'  DataSourceBehavior = 0   'vbNone
'  MTSTransactionMode = 0   'NotAnMTSObject
'End
'Attribute VB_Name = "CNetworkEnum"
'Attribute VB_GlobalNameSpace = False
'Attribute VB_Creatable = True
'Attribute VB_PredeclaredId = False
'Attribute VB_Exposed = False
'CNetworkEnum
'
Option Explicit
 
Private Const RESOURCE_CONNECTED As Long = &H1&
Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCE_REMEMBERED As Long = &H3&
 
Private Const RESOURCEDISPLAYTYPE_DIRECTORY& = &H9
Private Const RESOURCEDISPLAYTYPE_DOMAIN& = &H1
Private Const RESOURCEDISPLAYTYPE_FILE& = &H4
Private Const RESOURCEDISPLAYTYPE_GENERIC& = &H0
Private Const RESOURCEDISPLAYTYPE_GROUP& = &H5
Private Const RESOURCEDISPLAYTYPE_NETWORK& = &H6
Private Const RESOURCEDISPLAYTYPE_ROOT& = &H7
Private Const RESOURCEDISPLAYTYPE_SERVER& = &H2
Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
Private Const RESOURCEDISPLAYTYPE_SHAREADMIN& = &H8
 
Private Const RESOURCETYPE_ANY As Long = &H0&
Private Const RESOURCETYPE_DISK As Long = &H1&
Private Const RESOURCETYPE_PRINT As Long = &H2&
Private Const RESOURCETYPE_UNKNOWN As Long = &HFFFF&
 
Private Const RESOURCEUSAGE_ALL As Long = &H0&
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&
Private Const RESOURCEUSAGE_CONTAINER As Long = &H2&
Private Const RESOURCEUSAGE_RESERVED As Long = &H80000000
 
Private Const NO_ERROR = 0
Private Const ERROR_MORE_DATA = 234
Private Const RESOURCE_ENUM_ALL As Long = &HFFFF
 
Private Type NETRESOURCE
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    pLocalName As Long
    pRemoteName As Long
    pComment As Long
    pProvider As Long
End Type
 
Private Type NETRESOURCE_EXTENDED
    dwScope As Long
    dwType As Long
    dwDisplayType As Long
    dwUsage As Long
    sLocalName As String
    sRemoteName As String
    sComment As String
    sProvider As String
End Type
 
'WNet API resources
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
    (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, _
    ByVal dwFlags As Long) As Long
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, lpBuffer As NETRESOURCE, lpBufferSize As Long) As Long
Private Declare Function WNetCloseEnum Lib "mpr.dll" (ByVal hEnum As Long) As Long
Private Declare Function VarPtrAny Lib "vb40032.dll" Alias "VarPtr" (lpObject As Any) As Long
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (lpTo As Any, lpFrom As Any, _
    ByVal lLen As Long)
Private Declare Sub CopyMemByPtr Lib "kernel32" Alias "RtlMoveMemory" (ByVal lpTo As Long, _
    ByVal lpFrom As Long, ByVal lLen As Long)
Private Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal lpString1 As String, _
    ByVal lpString2 As Any) As Long
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As Any) As Long
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
    (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" _
    (ByVal lpBuffer As String, nSize As Long) As Long
 
'Filtered immediate access storage
Private sUserName As String
Private sMachineName As String
Private sServerList As String
Private sPrinterList As String
Private sShareList As String
Private sDirectoryList As String
Private sDomainList As String
Private sFileList As String
Private sGenericList As String
Private sGroupList As String
Private sNetworkList As String
Private sRootList As String
Private sShareAdminList As String
 
'Sets the resource types that will be enumerated (disk,printer etc)
Private lResType As Long
 
'Limitation
Private Const MAX_RESOURCES = 256
Private Const NOT_A_CONTAINER = -1
 
'Stores the results of the enumeration
Private uNetApi(0 To MAX_RESOURCES) As NETRESOURCE
Private uNet() As NETRESOURCE_EXTENDED
 
Private Sub Class_Initialize()
'Default setting
    Call SetResourceType(RESOURCETYPE_DISK)
End Sub
 
'Reloads the enumerated list of network entities
Public Sub Reset()
    Dim bFirstTime As Boolean
    Dim lReturn As Long
    Dim hEnum As Long
    Dim lCount As Long
    Dim lMin As Long
    Dim lLength As Long
    Dim l As Long
    Dim lBufferSize As Long
    Dim lLastIndex As Long
 
    bFirstTime = True
    Do
        'Create an enumeration using the required resource type
        If bFirstTime Then
            lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, GetResourceType, RESOURCEUSAGE_ALL, _
                ByVal 0&, hEnum)
            bFirstTime = False
        Else
            If uNet(lLastIndex).dwUsage And RESOURCEUSAGE_CONTAINER Then
                lReturn = WNetOpenEnum(RESOURCE_GLOBALNET, GetResourceType, RESOURCEUSAGE_ALL, _
                    uNet(lLastIndex), hEnum)
            Else
                lReturn = NOT_A_CONTAINER
                hEnum = 0
            End If
            lLastIndex = lLastIndex + 1
        End If
 
        'Make sure that we have a good enumeration
        If lReturn = NO_ERROR Then
            lCount = RESOURCE_ENUM_ALL
            'Work through the enumeration until we run out
            Do
                lBufferSize = UBound(uNetApi) * Len(uNetApi(0)) / 2
                lReturn = WNetEnumResource(hEnum, lCount, uNetApi(0), lBufferSize)
                If lCount > 0 Then
                    ReDim Preserve uNet(0 To lMin + lCount - 1) As NETRESOURCE_EXTENDED
                    For l = 0 To lCount - 1
                        'Each Resource will appear here as uNet(i)
                        uNet(lMin + l).dwScope = uNetApi(l).dwScope
                        uNet(lMin + l).dwType = uNetApi(l).dwType
                        uNet(lMin + l).dwDisplayType = uNetApi(l).dwDisplayType
                        uNet(lMin + l).dwUsage = uNetApi(l).dwUsage
 
                        'Get the name
                        If uNetApi(l).pLocalName Then
                            lLength = lstrlen(uNetApi(l).pLocalName)
                            uNet(lMin + l).sLocalName = Space$(lLength)
                            CopyMem ByVal uNet(lMin + l).sLocalName, ByVal uNetApi(l).pLocalName, _
                                lLength
                        End If
 
                        'Get the remote name
                        If uNetApi(l).pRemoteName Then
                            lLength = lstrlen(uNetApi(l).pRemoteName)
                            uNet(lMin + l).sRemoteName = Space$(lLength)
                            CopyMem ByVal uNet(lMin + l).sRemoteName, _
                                ByVal uNetApi(l).pRemoteName, lLength
                        End If
 
                        'Get any comment associated with it
                        If uNetApi(l).pComment Then
                            lLength = lstrlen(uNetApi(l).pComment)
                            uNet(lMin + l).sComment = Space$(lLength)
                            CopyMem ByVal uNet(lMin + l).sComment, ByVal uNetApi(l).pComment, _
                                lLength
                        End If
 
                        'Get the provider information
                        If uNetApi(l).pProvider Then
                            lLength = lstrlen(uNetApi(l).pProvider)
                            uNet(lMin + l).sProvider = Space$(lLength)
                            CopyMem ByVal uNet(lMin + l).sProvider, ByVal uNetApi(l).pProvider, _
                                lLength
                        End If
                    Next l
                End If
                lMin = lMin + lCount
            Loop While lReturn = ERROR_MORE_DATA
        End If
 
        'Check if we have a successfully opened Enumeration
        If hEnum Then
            l = WNetCloseEnum(hEnum)
        End If
 
    Loop While lLastIndex < lMin
 
    'Decode the results
    Call DecodeEnum
End Sub
 
Private Sub DecodeLocalInfo()
    On Error Resume Next
 
    'Create a buffer
    sUserName = String(255,  chr ( 0 ) )
 
    'Get the username
    Call GetUserName(sUserName, 255)
 
    'Strip the rest of the buffer
    sUserName = Left(sUserName, InStr(sUserName,  chr ( 0 ) ) - 1)
 
    'Create a buffer
    sMachineName = String(255,  chr ( 0 ) )
    Call GetComputerName(sMachineName, 255)
 
    'Remove the unnecessary  chr ( 0 ) 's
    sMachineName = Left$(sMachineName, InStr(1, sMachineName,  chr ( 0 ) ) - 1)
End Sub
 
'Sets the resource type to be enumerated
'(restricting this greatly improves performance)
'0 RESOURCETYPE_ANY = Everything
'1 RESOURCETYPE_DISK = Machines and shares only
'2 RESOURCETYPE_PRINT = Printers only
'Default is RESOURCETYPE_DISK
Public Sub SetResourceType(lResourceType As Long)
    lResType = lResourceType
End Sub
 
'Returns the current resource type that is being scanned for
Public Function GetResourceType() As Long
    GetResourceType = lResType
End Function
 
'Decodes the network array into a useful set of values
Private Sub DecodeEnum()
    Dim l As Long
    If UBound(uNet) > 0 Then
        'Get some local information
        Call DecodeLocalInfo
 
        'Parse the network enumeration
        For l = 0 To UBound(uNet)
            'TODO: Include comments? uNet(l).sComment
            Select Case uNet(l).dwDisplayType
            Case RESOURCEDISPLAYTYPE_DIRECTORY&
                sDirectoryList = sDirectoryList + uNet(l).sRemoteName + "|"
            Case RESOURCEDISPLAYTYPE_DOMAIN
                sDomainList = sDomainList + uNet(l).sRemoteName + ", "
            Case RESOURCEDISPLAYTYPE_FILE
                sFileList = sFileList + uNet(l).sRemoteName + "|"
            Case RESOURCEDISPLAYTYPE_GENERIC
                sGenericList = sGenericList + uNet(l).sRemoteName + "|"
            Case RESOURCEDISPLAYTYPE_GROUP
                sGroupList = sGroupList + uNet(l).sRemoteName + "|"
            Case RESOURCEDISPLAYTYPE_NETWORK&
                sNetworkList = sNetworkList + uNet(l).sRemoteName + "|"
            Case RESOURCEDISPLAYTYPE_ROOT&
                sRootList = sRootList + uNet(l).sRemoteName + "|"
            Case RESOURCEDISPLAYTYPE_SERVER
                sServerList = sServerList + uNet(l).sRemoteName + ","
            Case RESOURCEDISPLAYTYPE_SHARE
                sShareList = sShareList + uNet(l).sRemoteName + "|"
            Case RESOURCEDISPLAYTYPE_SHAREADMIN&
                sShareAdminList = sShareAdminList + uNet(l).sRemoteName + "|"
            End Select
        Next l
    End If
End Sub
 
'Provides the current user name
Public Function GetLocalUserName() As String
    GetLocalUserName = sUserName
End Function
 
'Provides the current machine name
Public Function GetLocalMachineName() As String
    GetLocalMachineName = sMachineName
End Function
 
'Returns a | delimited list of all computers on the network
Public Function GetServerList() As String
    GetServerList = sServerList
End Function
 
'Returns a | delimited list of all shares on the network
Public Function GetShareList() As String
    GetShareList = sShareList
End Function
 
'Returns a | delimited list of all printers on the network
Public Function GetPrinterList() As String
    GetPrinterList = sPrinterList
End Function
 
'Returns a | delimited list of all directories on the network
Public Function GetDirectoryList() As String
    GetDirectoryList = sDirectoryList
End Function
 
'Returns a | delimited list of all domains on the network
Public Function GetDomainList() As String
    GetDomainList = sDomainList
End Function
 
'Returns a | delimited list of all files on the network
Public Function GetFileList() As String
    GetFileList = sFileList
End Function
 
'Returns a | delimited list of all generic items on the network
Public Function GetGenericList() As String
    GetGenericList = sGenericList
End Function
 
'Returns a | delimited list of all groups on the network
Public Function GetGroupList() As String
    GetGroupList = sGroupList
End Function
 
'Returns a | delimited list of all sub networks on the network
Public Function GetNetworkList() As String
    GetNetworkList = sNetworkList
End Function
 
'Returns a | delimited list of all roots on the network
Public Function GetRootList() As String
    GetRootList = sRootList
End Function
 
'Returns a | delimited list of all admin shares on the network
Public Function GetShareAdminList() As String
    GetShareAdminList = sShareAdminList
End Function

Related posts:

  1. How to Get the list of all SQL Servers in the Network
  2. How to Display networked computers in a list box
  3. How to Connect Network Drive
  4. How to Disconnect Network Drive
  5. How to Check whether given path is a network path
  6. How to add Horizontal ScrollBar to list box control using API
  7. Get Time and Date of a Network machine Server or Workstation
  8. How to Trim all Non-printing Characters from a given String
  9. How to list down all necessary Drive information
  10. How to clear Documents List (under the Start button)

Comments (1) Trackbacks (0)
  1. thanks, it works


Leave a comment


No trackbacks yet.