|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Visual Basic Network Services NetConnectionEnum: Enumerating Share Connection Information |
|
Posted: | Tuesday May 22, 2001 |
Updated: | Monday December 26, 2011 |
Applies to: | VB4-32, VB5, VB6 |
Developed with: | VB6, Windows 2000 |
OS restrictions: | Windows NT3.1, Windows NT4, Windows 2000, Windows XP |
Author: | VBnet - Randy Birch |
Related: |
WNetAddConnection2: Transparently Connect to Network Shares NetShareCheck: Determine Remote Folder or Device Share Status WNetEnumResource: Enumerating Local Network Resources NetConnectionEnum: Enumerating Share Connection Information NetShareEnum: Enumerating Shared Resources on Other Machines NetShareAdd: Create a Local or Remote Share WNetGetUser: User, Share and Share User for Network Resources WNetGetConnection: Get UNC Path for Mapped Drive |
Prerequisites |
For this demo, one of the operating systems listed under OS Restrictions above. |
|
NetConnectionEnum lists all connections made to a shared resource on the server, or all connections established from a particular computer. If there is more than one user using this connection, then it is possible to get more than one structure for the same connection, but with a different user name.
The demo illustration shows the connections and open files on the local machine's C$ share, indicating that the local machine has one
connection (127.0.0.1), and that LAPTOP2000 has three connections to the share. On the local machine via the share there are seven files
opened, while the remote machine has twelve files open.
Both machines in the demo are running Windows 2000. When the demo is run from NT or 2000, the CONNECTION_INFO_1 data type is used to return information from the call, with the API as defined in this demo. Interrogating a Win9x machine from a 2000 machine using this data type also returns the correct information. When the method is called from a Windows 9x machine, the method must use the CONNECTION_INFO_50 data type and a modified set of API declaration parameters (provided in the declares below). The CONNECTION_INFO_50 data type only provides the user name, net name, time, number of open files and connection type. It does not provide the additional connection ID number nor the number of users connecting to the share. The CONNECTION_INFO_50 structure is not supported on NT/2000, and the CONNECTION_INFO_1 structure is not supported on Win 9x. In addition, when calling from 9x using the CONNECTION_INFO_50 structure, the caller must allocate and deallocate memory for the buffer before and after the call. On NT/2000, this is handled by the API via the MAX_PREFERRED_LENGTH flag, and the NetApiBufferFree API. The Win9x method of calling this API is not demonstrated. The first parameter of either API declare points to the string
representing the remote server on which the function is to execute. If this is null (vbNullString), the local computer is used. The
second parameter specifies the share of interest. Only one share at a time can be enumerated; the method above shows the connection
information for the C$ share only. The string passed as the qualifier parameter is the "friendly" share name provided when the
share was created, i.e. "C$", "zips", "shared docs" etc. It is also necessary to specify, as the third
parameter, an identifier to the "level" information structure being passed, in this demo a level 1 structure. |
BAS Module Code |
None. |
|
Form Code |
To a form add a command button (Command1), list box (List1), and two labels (Label1, Label2). A third set of labels in a control array can be used for the list item captions. Add the following to the form: |
|
Option Explicit '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Copyright ©1996-2011 VBnet/Randy Birch, All Rights Reserved. ' Some pages may also contain other copyrights by the author. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Distribution: You can freely use this code in your own ' applications, but you may not reproduce ' or publish this code on any web site, ' online service, or distribute as source ' on any media without express permission. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Private Const NERR_SUCCESS As Long = 0& Private Const MAX_PREFERRED_LENGTH As Long = -1 Private Const ERROR_MORE_DATA As Long = 234& Private Const LB_SETTABSTOPS As Long = &H192 Private Const STYPE_DISKTREE = 0 Private Const STYPE_PRINTQ = 1 Private Const STYPE_DEVICE = 2 Private Const STYPE_IPC = 3 'for use on Win NT/2000 only Private Type CONNECTION_INFO_1 coni1_id As Long coni1_type As Long coni1_num_opens As Long coni1_num_users As Long coni1_time As Long coni1_username As Long coni1_netname As Long End Type Private Declare Function NetConnectionEnum Lib "netapi32" _ (ByVal servername As Long, _ ByVal qualifier As Long, _ ByVal level As Long, _ bufptr As Long, _ ByVal prefmaxlen As Long, _ entriesread As Long, _ totalentries As Long, _ resume_handle As Long) As Long Private Declare Function NetApiBufferFree Lib "netapi32" _ (ByVal Buffer As Long) As Long 'common routines Private Declare Sub CopyMemory Lib "kernel32" _ Alias "RtlMoveMemory" _ (pTo As Any, uFrom As Any, _ ByVal lSize As Long) Private Declare Function lstrlenW Lib "kernel32" _ (ByVal lpString As Long) As Long Private Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Private Sub Form_Load() ReDim TabArray(0 To 5) As Long TabArray(0) = 59 TabArray(1) = 128 TabArray(2) = 159 TabArray(3) = 185 TabArray(4) = 212 TabArray(5) = 243 'Clear existing tabs and set Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 0&, ByVal 0&) Call SendMessage(List1.hwnd, LB_SETTABSTOPS, 6&, TabArray(0)) List1.Refresh Command1.Caption = "NetConnectionEnum" Label1.Caption = "call success (0) or error :" Label2.Caption = "" End Sub Private Sub Command1_Click() Dim bufptr As Long 'output Dim dwServer As Long 'pointer to the server Dim dwShare As Long 'pointer to the share Dim dwEntriesread As Long 'out Dim dwTotalentries As Long 'out Dim dwResumehandle As Long 'out Dim success As Long Dim nStructSize As Long Dim cnt As Long Dim bServer As String Dim bShare As String Dim ci1 As CONNECTION_INFO_1 'use the local machine for testing. This parameter can 'specify the local machine or the server upon which 'the enumeration should occur. Note that COMPUTERNAME 'is an NT/2000 environment variable. bServer = "\\" & Environ$("COMPUTERNAME") & vbNullString dwServer = StrPtr(bServer) bShare = "C$" dwShare = StrPtr(bShare) 'this is the NT/2000 declare being called! success = NetConnectionEnum(dwServer, _ dwShare, _ 1, _ bufptr, _ MAX_PREFERRED_LENGTH, _ dwEntriesread, _ dwTotalentries, _ dwResumehandle) List1.Clear Label2.Caption = success If success = NERR_SUCCESS And _ success <> ERROR_MORE_DATA Then nStructSize = LenB(ci1) For cnt = 0 To dwEntriesread - 1 'get one chunk of data and cast 'into an CONNECTION_INFO_1 type, 'and add the data to a list CopyMemory ci1, ByVal bufptr + (nStructSize * cnt), nStructSize 'ci1.coni1_time returns the number of seconds; 'here we translate that into minutes List1.AddItem GetPointerToByteStringW(ci1.coni1_username) & vbTab & _ GetPointerToByteStringW(ci1.coni1_netname) & vbTab & _ ci1.coni1_time \ 60 & vbTab & _ ci1.coni1_num_opens & vbTab & _ ci1.coni1_num_users & vbTab & _ ci1.coni1_id & vbTab & _ GetConnectionType(ci1.coni1_type) Next End If Call NetApiBufferFree(bufptr) End Sub Private Function GetPointerToByteStringW(ByVal dwData As Long) As String Dim tmp() As Byte Dim tmplen As Long If dwData <> 0 Then tmplen = lstrlenW(dwData) * 2 If tmplen <> 0 Then ReDim tmp(0 To (tmplen - 1)) As Byte CopyMemory tmp(0), ByVal dwData, tmplen GetPointerToByteStringW = tmp End If End If End Function Private Function GetConnectionType(ByVal dwSessionType As Long) As String Select Case dwSessionType Case STYPE_DISKTREE: GetConnectionType = "Disk drive" Case STYPE_PRINTQ: GetConnectionType = " Print queue" Case STYPE_DEVICE: GetConnectionType = "Communication device" Case STYPE_IPC: GetConnectionType = "(IPC)" Case Else: GetConnectionType = "" End Select End Function |
Comments |
|
|
|
|||||
|
|||||
|
|||||
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |