private IReadOnlyList <SidName> MapNames(string[] names, SafeSamMemoryBuffer rid_buffer, SafeSamMemoryBuffer use_buffer)
        {
            using (rid_buffer)
            {
                using (use_buffer)
                {
                    rid_buffer.Initialize <uint>((uint)names.Length);
                    use_buffer.Initialize <int>((uint)names.Length);
                    uint[] rids = rid_buffer.ReadArray <uint>(0, names.Length);
                    int[]  use  = use_buffer.ReadArray <int>(0, names.Length);

                    return(names.Select((n, i) => new SidName(DomainId.CreateRelative(rids[i]),
                                                              Name, n.ToString(), SidNameSource.Account, (SidNameUse)use[i], false))
                           .Where(n => n.NameUse != SidNameUse.Unknown).ToList().AsReadOnly());
                }
            }
        }
        private T CreateObject <T>(SafeSamHandle handle, uint user_id, string name, Func <string, Sid, T> func)
        {
            try
            {
                Sid sid = RidToSid(user_id, false).GetResultOrDefault();
                if (sid == null)
                {
                    sid = DomainId.CreateRelative(user_id);
                }

                if (name == null)
                {
                    name = LookupId(user_id, false).GetResultOrDefault()?.Name ?? sid.ToString();
                }

                return(func(name, sid));
            }
            catch
            {
                handle.Dispose();
                throw;
            }
        }
        private SamUser CreateUserObject(SafeSamHandle user_handle, SamUserAccessRights desired_access, string name, uint user_id)
        {
            try
            {
                Sid sid = RidToSid(user_id, false).GetResultOrDefault();
                if (sid == null)
                {
                    sid = DomainId.CreateRelative(user_id);
                }

                if (name == null)
                {
                    name = LookupId(user_id, false).GetResultOrDefault()?.Name ?? sid.ToString();
                }

                return(new SamUser(user_handle, desired_access, ServerName, name, sid));
            }
            catch
            {
                user_handle.Dispose();
                throw;
            }
        }