First we'll create a class to process WinApi calls.
This Class has been written by Michael Bright in 2003.Since then i've been using this code and extending it for network related needs.
- using System;
- using System.Runtime.InteropServices;
- namespace GettingAllUsers {
- public class NetworkAPI {
-
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct USER_INFO_1 {
- public string usri1_name;
- public string usri1_password;
- public int usri1_password_age;
- public int usri1_priv;
- public string usri1_home_dir;
- public string comment;
- public int usri1_flags;
- public string usri1_script_path;
- }
-
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct USER_INFO_0 {
- public String Username;
- }
-
- [DllImport("Netapi32.dll")]
- public extern static int NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, int level, ref USER_INFO_1 buf, int parm_err);
-
- [DllImport("Netapi32.dll")]
- public extern static int NetUserDel([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username);
-
- [DllImport("Netapi32.dll")]
- public extern static int NetUserGetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, out IntPtr bufptr);
-
- [DllImport("Netapi32.dll")]
- public extern static int NetUserSetInfo([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username, int level, ref USER_INFO_1 buf, int error);
-
- [DllImport("Netapi32.dll")]
- public extern static int NetUserChangePassword([MarshalAs(UnmanagedType.LPWStr)] string domainname, [MarshalAs(UnmanagedType.LPWStr)] string username, [MarshalAs(UnmanagedType.LPWStr)] string oldpassword, [MarshalAs(UnmanagedType.LPWStr)] string newpassword);
-
- [DllImport("Netapi32.dll")]
- public extern static int NetUserEnum(string servername, int level, int filter, out IntPtr bufptr, int prefmaxlen, out int entriesread, out int totalentries, out int resume_handle);
-
- [DllImport("Netapi32.dll")]
- public extern static int NetApiBufferFree(IntPtr Buffer);
- public NetworkAPI() {
-
-
-
- }
- }
- }
Add a ListBox in your form and then,Later inside your main application create a function:
- public void EnumerateUsers() {
- int EntriesRead;
- int TotalEntries;
- int Resume;
- listBox1.Items.Clear();
- IntPtr bufPtr;
- NetworkAPI.NetUserEnum(null, 0, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume);
- if (EntriesRead > 0) {
- NetworkAPI.USER_INFO_0[] Users = new NetworkAPI.USER_INFO_0[EntriesRead];
- IntPtr iter = bufPtr;
- for (int i = 0; i < EntriesRead; i++) {
- Users[i] = (NetworkAPI.USER_INFO_0) Marshal.PtrToStructure(iter, typeof(NetworkAPI.USER_INFO_0));
- iter = (IntPtr)((int) iter + Marshal.SizeOf(typeof(NetworkAPI.USER_INFO_0)));
- listBox1.Items.Add(Users[i].Username);
- }
- NetworkAPI.NetApiBufferFree(bufPtr);
- }
- listBox1.SelectedIndex = 0;
- }
- Now just call EnumerateUsers inside Form_Load to populate them in your listbox at startup
- private void Form1_Load(object sender, EventArgs e) {
- EnumerateUsers();
- }
And run it!
Hope that helps!