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

NetUserEnum (netapi32)
 
.
Summary
The NetUserEnum function provides information about all user accounts on a server.

C# Signature:

[DllImport("Netapi32.dll")]

extern static int NetUserEnum([MarshalAs(UnmanagedType.LPWStr)]

string servername,

int level,

int filter,

out IntPtr bufptr,

int prefmaxlen,

out int entriesread,

out int totalentries,

out int resume_handle);

VB Signature:

    Public Declare Function NetUserEnum Lib "Netapi32.dll" ( _
    <MarshalAs(UnmanagedType.LPWStr)> ByVal servername As String, _
    ByVal level As Integer, _
    ByVal filter As Integer, _
    ByRef bufptr As IntPtr, _
    ByVal prefmaxlen As Integer, _
    ByRef entriesread As Integer, _
    ByRef totalentries As Integer, _
    ByRef resume_handle As Integer) As Integer

User-Defined Types:

None.

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

[DllImport("netapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]

        private static extern int NetApiBufferFree (System.IntPtr pBuffer);

        [DllImport("Netapi32.dll")]
        extern static int NetUserEnum([MarshalAs(UnmanagedType.LPWStr)]
            string servername,
            int level,
            int filter,
            out IntPtr bufptr,
            int prefmaxlen,
            out int entriesread,
            out int totalentries,
            out int resume_handle);

        [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
            public struct USER_INFO_2
        {
            public String usr_name;  
            public String usri2_password;  
            public int usri2_password_age;  
            public int usri2_priv;  
            public String usri2_home_dir;  
            public String usri2_comment;  
            public int usri2_flags;  
            public String usri2_script_path;  
            public int usri2_auth_flags;  
            public String usri2_full_name;  
            public String usri2_usr_comment;  
            public String usri2_parms;  
            public String usri2_workstations;  
            public int usri2_last_logon;  
            public int usri2_last_logoff;  
            public int usri2_acct_expires;  
            public int usri2_max_storage;  
            public int usri2_units_per_week;  
            public int usri2_logon_hours;  
            public int usri2_bad_pw_count;  
            public int usri2_num_logons;  
            public string usri2_logon_server;  
            public int usri2_country_code;  
            public int usri2_code_page;

        }

public static Hashtable GetUsersListWithNETAPI(string strServerName)

        {
            //with struct USER_INFO_2
            ArrayList users = new ArrayList();
            Hashtable userlist=new Hashtable();
            int EntriesRead;
            int TotalEntries;
            int Resume;
            IntPtr bufPtr;
            string strServer;
            if (strServerName.Trim()!="")
            {
                strServer=@"\\"+strServerName;
            }
            else
            {
                strServer=strServerName;
            }
            try
            {
                NetUserEnum(strServer, 2, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
                if(EntriesRead> 0)
                {
                    USER_INFO_2[] Users = new USER_INFO_2[EntriesRead];
                    IntPtr iter = bufPtr;
                    for(int i=0; i < EntriesRead; i++)
                    {
                        Users[i] = (USER_INFO_2)Marshal.PtrToStructure(iter, typeof(USER_INFO_2));
                        iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_2)));

                        //users.Add(Users[i].usr_name);
                        userlist.Add(Users[i].usr_name,Users[i].usri2_full_name);

                    }
                    NetApiBufferFree(bufPtr);

                }
            }
            catch(Exception Ex)
            {
                Console.WriteLine(Ex.Message);
                return null;
            }
            return userlist;
        }
    }

}

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
NetUserEnum on MSDN

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
Find References
Show Printable Version
Revisions