|
|
![]() |
|
||
|
|
|||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
| Visual Basic Network Services NetLocalGroupEnum: Enumerate Local Groups |
||
| Posted: | Thursday April 19, 2001 | |
| Updated: | Monday December 26, 2011 | |
| Applies to: | VB4-32, VB5, VB6 | |
| Developed with: | VB6, Windows 2000 | |
| OS restrictions: | Windows NT4 / Windows 2000, Windows XP | |
| Author: | VBnet - Randy Birch | |
|
Related: |
NetLocalGroupEnum: Enumerate Local Groups and Descriptions | |
| Prerequisites | ||||||
| Network connection to a domain controller, and/or one of the operating systems listed above. | ||||||
|
|
||||||
NetLocalGroupEnum,
one of Windows Network Management Functions, returns information about each local group account on the specified server. The
LOCALGROUP_INFO_0 structure used with this API has only one member - the group name.
On Windows NT, no special group membership is required to successfully execute
the NetLocalGroupEnum function. However, on Windows 2000/XP, if you call this function on a Windows 2000/XP domain controller running Active
Directory, access is allowed or denied based on the access-control list (ACL) for the securable object. The default ACL permits all
authenticated users and members of the "Pre-Windows 2000 compatible access" group to view the information. By default, the
"Pre-Windows 2000 compatible access" group includes Everyone as a member. This enables anonymous access to the information if the
system allows anonymous access. servername [in] Pointer to a constant Unicode string specifying the name of the remote server on which the function is to execute. The string must begin with \\. If this parameter is NULL, the local computer is used level [in] Specifies the information level of the data. This parameter can be one of the following values.
bufptr [out] Pointer to the address of the buffer that receives the information structure. The format of this data depends on the value of the level parameter. This buffer is allocated by the system and must be freed using the NetApiBufferFree function. Note that you must free the buffer even if the function fails with ERROR_MORE_DATA. prefmaxlen [in] Specifies the preferred maximum length of returned data, in bytes. If you specify MAX_PREFERRED_LENGTH, the function allocates the amount of memory required for the data. If you specify another value in this parameter, it can restrict the number of bytes that the function returns. If the buffer size is insufficient to hold all entries, the function returns ERROR_MORE_DATA. For more information, see Network Management Function Buffers and Network Management Function Buffer Lengths in the MSDN. entriesread [out] Pointer to a DWORD value that receives the count of elements actually enumerated. totalentries [out] Pointer to a DWORD value that receives the approximate total number of entries that could have been enumerated from the current resume position. For more information, see the following Remarks section. resumehandle [in/out] Pointer to a value that contains a resume handle that is used to continue an existing local group search. The handle should be zero on the first call and left unchanged for subsequent calls. If this parameter is NULL, then no resume handle is stored. For more information, see the following Remarks section. Return Values If the function succeeds, the return value is NERR_Success. If the function fails, the return value can be one of the following error codes: ERROR_ACCESS_DENIED, ERROR_MORE_DATA, NERR_InvalidComputer or NERR_BufTooSmall. |
||||||
| BAS Module Code | ||||||
| None. | ||||||
|
|
||||||
| Form Code | ||||||
|
|
||||||
| To a form, add a listview (ListView1) and a command button (Command1). Add the following code: | ||||||
|
|
||||||
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. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 'Windows type used to call the Net API
Private Const MAX_PREFERRED_LENGTH As Long = -1
Private Const NERR_SUCCESS As Long = 0&
Private Const ERROR_MORE_DATA As Long = 234&
Private Type LOCALGROUP_INFO_0
lgrpi1_name As Long
End Type
Private Declare Function NetLocalGroupEnum Lib "netapi32" _
(servername As Byte, _
ByVal Level As Long, _
buff As Long, _
ByVal buffsize As Long, _
entriesread As Long, _
totalentries As Long, _
resumehandle As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32" _
(ByVal lpBuffer As Long) As Long
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 Sub Form_Load()
Command1.Caption = "Enum Local Groups"
With ListView1
.ColumnHeaders.Add , , "Group Name"
.View = lvwReport
.FullRowSelect = True
End With
End Sub
Private Sub Command1_Click()
Dim bServer() As Byte
Dim tmp As String
ListView1.ListItems.Clear
bServer = "\\workstation" & vbNullChar
'required to show server in caption
tmp = bServer
Label1.Caption = GetLocalGroups(bServer) & " local groups found on " & tmp
End Sub
Private Function GetLocalGroups(bServer() As Byte) As Long
Dim bufptr As Long
Dim entriesread As Long
Dim totalentries As Long
Dim success As Long
Dim resumehandle As Long
Dim nStructSize As Long
Dim cnt As Long
Dim itmx As ListItem
Dim lgis As LOCALGROUP_INFO_0
nStructSize = Len(lgis)
success = NetLocalGroupEnum(bServer(0), _
0, bufptr, _
MAX_PREFERRED_LENGTH, _
entriesread, _
totalentries, _
resumehandle)
If success = NERR_SUCCESS And _
success <> ERROR_MORE_DATA Then
For cnt = 0 To entriesread - 1
'get one chunk of data and cast
'into an LOCALGROUP_INFO_1 type, and
'add the group name to a list
CopyMemory lgis, ByVal bufptr + (nStructSize * cnt), nStructSize
'add the group name to the listview
Set itmx = ListView1.ListItems.Add(, , GetPointerToByteStringW(lgis.lgrpi1_name))
Next
End If
Call NetApiBufferFree(bufptr)
GetLocalGroups = entriesread
End Function
Public 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
|
||||||
| Comments | ||||||
|
|
|
|
|
|||||
|
|||||
|
|
|||||
|
Copyright ©1996-2011 VBnet and Randy Birch. All Rights Reserved. |
![]() |