public static IEnumerable <DomainUser> EnumLocalUsers(bool sid = false) { int read; int total; int resume; IntPtr buf; int ret = netapi.NetUserEnum(null, 10, util.FILTER_NORMAL_ACCOUNT, out buf, util.MAX_PREFERRED_LENGTH, out read, out total, out resume); if (ret != 0) { throw new Win32Exception(ret); } if (read > 0) { DomainUser user = new DomainUser(); var u = new USER_INFO_10(); IntPtr pItem = buf; for (int i = 0; i < read; ++i) { Marshal.PtrToStructure(pItem, u); pItem = new IntPtr(pItem.ToInt64() + Marshal.SizeOf(typeof(USER_INFO_10))); if (sid) { user.SID = GetAccountSID(u.name); } user.Name = u.name; user.FullName = u.full_name; user.Comment = u.comment; yield return(user); } netapi.NetApiBufferFree(buf); } }
public bool NetUseGetUserInfo(ref string FullName) { uint returncode; try { USER_INFO_10 objUserInfo10 = new USER_INFO_10(); IntPtr bufPtr; returncode = NetUserGetInfo(sDomain, sUser, 10, out bufPtr); if (returncode == 0) { objUserInfo10 = (USER_INFO_10)Marshal.PtrToStructure(bufPtr, typeof(USER_INFO_10)); FullName = string.Format(" Full Name = {0}, Comment = {1}, User Name = {2}, User Comment = {3}", objUserInfo10.usri10_full_name, objUserInfo10.usri10_comment, objUserInfo10.usri10_name, objUserInfo10.usri10_usr_comment); } iLastError = (int)returncode; return(returncode == 0); } catch { iLastError = Marshal.GetLastWin32Error(); return(false); } }
public static ArrayList GetDomainUserList(string server) { ArrayList users = new ArrayList(); int EntriesRead; int TotalEntries; int Resume; IntPtr bufPtr; NetUserEnum(server, 10, 2, out bufPtr, -1, out EntriesRead, out TotalEntries, out Resume); if(EntriesRead> 0) { USER_INFO_10[] Users = new USER_INFO_10[EntriesRead]; IntPtr iter = bufPtr; for(int i=0; i < EntriesRead; i++) { Users[i] = (USER_INFO_10)Marshal.PtrToStructure(iter, typeof(USER_INFO_10)); iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(USER_INFO_10))); users.Add(Users[i].usri10_name); } NetApiBufferFree(bufPtr); } return(users); }