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

NetUserGetGroups (netapi32)
 
.
Summary
Retrieves a list of Net (Domain) groups that the given user on the local or remote computer belongs to.

C# Signature:

        [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,
             UInt32 prefmaxlen,
             int prefmaxlen,
             out int entriesread,
             out int totalentries);

VB Signature:

        <DllImport("Netapi32.dll", SetLastError := True)> _
        Public Shared Function NetUserGetGroups(
            <MarshalAs(UnmanagedType.LPWStr)> ByVal servername As String, _
            <MarshalAs(UnmanagedType.LPWStr)> ByVal username As String, _
            ByVal level 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)]
    internal struct LOCALGROUP_USERS_INFO_0
    {
        [MarshalAs(UnmanagedType.LPWStr)]internal string name;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct LOCALGROUP_USERS_INFO_1
    {
        [MarshalAs(UnmanagedType.LPWStr)] public string name;
        [MarshalAs(UnmanagedType.LPWStr)] public string comment;
    }

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

When the data to receive is larger than the specified in 'prefmaxlen', an error code 234 is returned. You can pass 0xFFFFFFFF (which required the change from int to UInt32) in this parameter and then the needed amount of memory will be allocated.

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 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,
             UInt32 prefmaxlen,
             out int entriesread,
             out int totalentries);

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

        #endregion

Alternative Managed API:

Do you know one? Please contribute it!

        private int ErrorCode;
        private string _ErrorMessage;

Notes:

None.

        public string ErrorMessage {
            get { return this._ErrorMessage; }
        }

Tips & Tricks:

Please add some!

        private ArrayList GetUserNetGroups(string ServerName,string Username, int Flags)

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 int ErrorCode;
        private string _ErrorMessage;

        public string ErrorMessage {
            get { return this._ErrorMessage; }
        }

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

            ErrorCode = NetUserGetGroups(ServerName,Username,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.GetUserNetGroups(null,"administrator",0);
            foreach(string str in RetGroups)
            {
                Console.WriteLine(str);
            }

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