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
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
netlocalgroupgetmembers (netapi32)
 
.
Summary

Code to read the membership of Local Groups (ideally used with NetLocalGroupEnum )

C# Signature:

        /// <summary>
        /// The NetLocalGroupGetMembers function retrieves a list of the members of a particular local group in
        /// the security database, which is the security accounts manager (SAM) database or, in the case
        /// of domain controllers, the Active Directory. Local group members can be users or global groups.
        /// </summary>
        /// <param name="servername"></param>
        /// <param name="localgroupname"></param>
        /// <param name="level"></param>
        /// <param name="bufptr"></param>
        /// <param name="prefmaxlen"></param>
        /// <param name="entriesread"></param>
        /// <param name="totalentries"></param>
        /// <param name="resume_handle"></param>
        /// <returns></returns>
        [DllImport("NetAPI32.dll", CharSet=CharSet.Unicode)]
        public extern static int NetLocalGroupGetMembers(
            [MarshalAs(UnmanagedType.LPWStr)] string servername,
            [MarshalAs(UnmanagedType.LPWStr)] string localgroupname,
            int level,
            out IntPtr bufptr,
            int prefmaxlen,
            out int entriesread,
            out int totalentries,
            IntPtr resume_handle);

VB Signature:

Declare Function NetLocalGroupGetMembers Lib "netapi32.dll" (TODO) As TODO

User-Defined Types:

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
            public struct LOCALGROUP_MEMBERS_INFO_2
        {
            public int lgrmi2_sid;
            public int lgrmi2_sidusage;
            public string lgrmi2_domainandname;
        }

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

        //Example code for a class file or dll( I used a dll)
        public static ArrayList GetLocalGroupMembers(string ServerName,string GroupName)
        {
            ArrayList myList = new ArrayList();
            int EntriesRead;
            int TotalEntries;
            int Resume  = IntPtr.Zero;
            IntPtr buffer;
            int val = NetLocalGroupGetMembers(ServerName, GroupName, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, Resume);
            if(EntriesRead> 0)
            {
                LOCALGROUP_MEMBERS_INFO_2[] Members = new LOCALGROUP_MEMBERS_INFO_2[EntriesRead];
                IntPtr iter = bufPtr;
                for(int i=0; i < EntriesRead; i++)
                {
                    Members[i] = (LOCALGROUP_MEMBERS_INFO_2)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_MEMBERS_INFO_2));
                    iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_MEMBERS_INFO_2)));
                    myList.Add(Members[i].lgrmi2_domainandname + "," + Members[i].lgrmi2_sidusage);
                }
                NetApiBufferFree(bufPtr);
            }
            return myList;
        }

////////////////////////////////////////////////////////////////////////////////////////////////////////

        //Example code for calling application
        ArrayList RetGroups = new ArrayList();
        RetGroups =DsDomain.GetLocalGroupMembers(null,"administrators");        
        string delimStr = ",";
        char [] delimiter = delimStr.ToCharArray();    
        foreach(string str in RetGroups)
    {        //I used this to retrieve the type of group so I could use an icon in a treeview if desired.
            //split[0] is the "domain\group or user", split[1] is the type, 1 for user, 2 for group.                
        string [] split = null;
        split = str.Split(delimiter);
        Console.WriteLine(split[0] + split[1]);
    }

Alternative Managed API:

Do you know one? Please contribute it!

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