netusergetlocalgroups (netapi32)
Last changed: -66.192.63.2

.
Summary
Retrieves a list of Local groups that the given user on the local or remote computer belongs to.

C# Signature:

        [DllImport("Netapi32.dll", SetLastError=true)]
        public extern static int NetUserGetLocalGroups([MarshalAs(UnmanagedType.LPWStr)] string servername,
            [MarshalAs(UnmanagedType.LPWStr)] string username,
            int level,
            int flags,
            out IntPtr bufptr,
            int prefmaxlen,
            out int entriesread,
            out int totalentries);

VB Signature:

        <DllImport("Netapi32.dll", SetLastError := True)> _
        Public Shared Function NetUserGetLocalGroups(
            <MarshalAs(UnmanagedType.LPWStr)> ByVal servername As String,
            <MarshalAs(UnmanagedType.LPWStr)> ByVal username As String, _
            ByVal level As Integer, _
            ByVal flags As Integer, _
            ByRef bufptr As IntPtr, _
            ByVal prefmaxlen As Integer, _
            ByRef entriesread As Integer, _
            ByRef totalentries As Integer) As Integer
        End Function

User-Defined Types:

        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
            public struct LOCALGROUP_USERS_INFO_0
        {
            public string groupname;
        }

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

        //Example code to place in a class.
        #region  pInvoke Items

        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
        public struct LOCALGROUP_USERS_INFO_0
        {
            public string groupname;
        }

        [DllImport("Netapi32.dll", SetLastError=true)]
        public extern static int NetUserGetGroups
            ([MarshalAs(UnmanagedType.LPWStr)] string servername,
             [MarshalAs(UnmanagedType.LPWStr)] string username,
             int level,
             out IntPtr bufptr,
             int prefmaxlen,
             out int entriesread,
             out int totalentries);

        [DllImport("Netapi32.dll", SetLastError=true)]
        public static extern int NetApiBufferFree(IntPtr Buffer);

        #endregion

        private ArrayList GetUserLocalGroups(string ServerName,string Username, int Flags)
        {
            ArrayList myList = new ArrayList();
            int EntriesRead;
            int TotalEntries;
            IntPtr bufPtr;

            ErrorCode = NetUserGetLocalGroups(ServerName,Username,0,Flags,out bufPtr,1024,out EntriesRead, out TotalEntries);
            if(ErrorCode==0)
            {
                _ErrorMessage="Successful";
            }
            else
            {
                _ErrorMessage="Username or computer not found";
            }
            if(Flags>1)
                _ErrorMessage="Flags can only be 0 or 1";
            if(EntriesRead> 0)
            {
                LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[EntriesRead];
                IntPtr iter = bufPtr;
                for(int i=0; i < EntriesRead; i++)
                {
                    RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0));
                    iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0)));
                    myList.Add(RetGroups[i].groupname);
                }
                NetApiBufferFree(bufPtr);
            }
            return myList;
        }

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

        //Example code for calling application
            ArrayList RetGroups = new ArrayList();
            RetGroups =DsDomain.GetUserLocalGroup(null,"administrator",0);
            foreach(string str in RetGroups)
            {
                Console.WriteLine(str);
            }

Documentation