Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

NetlGroupEnum (netapi32)
 
.
Summary
The NetGroupEnum function retrieves information about each global group in the security database, which is the security accounts manager (SAM) database or, in the case of domain controllers, the Active Directory.

C# Signature:

        [DllImport("Netapi32.dll")]
        internal extern static int NetGroupEnum
            ([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 NetlGroupEnum Lib "netapi32.dll" (TODO) As TODO

User-Defined Types:

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
    internal struct GROUP_INFO_0
    {
        [MarshalAs(UnmanagedType.LPWStr)]internal string grpi0_name;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct GROUP_INFO_1
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string grpi1_name;
        [MarshalAs(UnmanagedType.LPWStr)] public string grpi1_comment;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

This page is a modified copy/paste from the NetLocalGroupEnum.

Tips & Tricks:

The largest challenge is marshaling 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 marshaling returned data stored in more than one of the pointers to the array of structures which the output is placed in!

Sample Code:

        private string [] GetAllGroups(string ServerName) {
            int size=1024;    //Start with 1k
            IntPtr bufptr=new IntPtr(size);
            int level=0;
            int prefmaxlen=1023;
            int entriesread=0;
            int totalentries=0;
            int resume_handle=0;
            int err=0;
            do
            {
                err= NativeMethods.NetGroupEnum(
                    ServerName,
                    level,
                    out bufptr,
                    prefmaxlen,
                    out entriesread,
                    out totalentries,
                    ref resume_handle
                );
                switch(err)
                {
                    //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 0:        //NERR_Success
                    default:
                        break;
                }
            }
            while(err==234);    // and start over

            //GROUP_INFO_0 group=new GROUP_INFO_0(); //See user type above
            GROUP_INFO_0 group;
            string [] ret = new string[totalentries];
            IntPtr iter = bufptr;

            for(int i=0; i < totalentries; i++)
            {
                group = (GROUP_INFO_0)Marshal.PtrToStructure(iter, typeof(GROUP_INFO_0));
                ret[i] = group.grpi0_name;
                iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(GROUP_INFO_0)));
            }

            return ret;
        }

Documentation

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).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions