Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than netapi32, prefix the name with the module name and a period.
NetLocalGroupEnum (netapi32)
.
C# Signature:
[DllImport("Netapi32.dll")]
extern static int NetLocalGroupEnum([MarshalAs(UnmanagedType.LPWStr)]
string servername,
int level,
out IntPtr bufptr,
int prefmaxlen,
out int entriesread,
out int totalentries,
ref int resume_handle);
VB Signature:
Declare Function NetLocalGroupEnum Lib "netapi32.dll" (TODO) As TODO
User-Defined Types:
Managed version of the LOCALGROUP_INFO_1 structure:
[StructLayout(LayoutKind.Sequential)]
private struct GroupInfo
{
[MarshalAs(UnmanagedType.LPWStr)] public string name;
[MarshalAs(UnmanagedType.LPWStr)] public string comment;
}
Notes:
None.
Tips & Tricks:
Largest challenge is marshalling the pointer to the array of structures which the output is placed in.
In this example, I use a do/while which doubles the size of the buffer until it is large enough to contain all of the groups that are on the server. The API call is designed so that you can read sequentially (ERROR_MORE_DATA), but I didn't want to deal with marshalling returned data from more than one pointer to the array of structures which the output is placed in!
Sample Code:
int size =1024; //Start with 1k
IntPtr bufptr =new IntPtr(size);
int level =1;
int prefmaxlen =1023;
int entriesread =0;
int totalentries =0;
int resume_handle =0;
int err =0;
do
{
switch(err=NetLocalGroupEnum(
m_Server,
level,
out bufptr,
prefmaxlen,
out entriesread,
out totalentries,
ref resume_handle
))
{
//If there is more data, double the size of the buffer...
case 2123 : //NERR_BufTooSmall
case 234 : //ERROR_MORE_DATA
size*=2;
bufptr=new IntPtr(size);
prefmaxlen=size-1; //Increase the size you want read as well
resume_handle=0; //And reset the resume_handle or you'll just pick up where you left off.
break;
case 2351 : //NERR_InvalidComputer
//case 2123 : //ERROR_ACCESS_DENIED
case 0 : //NERR_Success
default : break;
}
}
while(err==234); // and start over
GroupInfo[] group=new GroupInfo(totalentries); //See user type above
IntPtr iter = bufptr;
for(int i=0; i < totalentries; i++)
{
group[totalentries] = (GroupInfo)Marshal.PtrToStructure(iter, typeof(GroupInfo));
iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(GroupInfo)));
}
Alternative Managed API:
Do you know one? Please contribute it!
Returns information about each local group account on the specified server.
6/19/2020 1:39:26 PM - -167.220.4.27
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).