public static LSA_UNICODE_STRING InitLSAString(string val)
        {
            LSA_UNICODE_STRING lsastring = new LSA_UNICODE_STRING();

            lsastring.Buffer = val;

            lsastring.Length = (UInt16)(val.Length * sizeof(char));

            lsastring.MaximumLength = (UInt16)(lsastring.Length + sizeof(char));

            return(lsastring);
        }
        protected void LoadInternal(string target)
        {
            this.Messages = new List <string> {
            };

            this.Privileges = new List <PrivilegeAndPrincipals> {
            };

            this.IsDisposed = false;

            this.Success = false;

            this.PolicyHandle = IntPtr.Zero;

            IntPtr temphandle = IntPtr.Zero;

            LSA_OBJECT_ATTRIBUTES lsainfo = NativeHelper.InitLSAObjectAttributes();

            LSA_UNICODE_STRING systemname = (target == null) ? default(LSA_UNICODE_STRING) : NativeHelper.InitLSAString(target);

            uint rc = Advapi32.LsaOpenPolicy(systemname, ref lsainfo, LSA_POLICY_ACCESS.POLICY_ALL_ACCESS, out temphandle);

            this.Success = (rc == 0);

            if (this.Success)
            {
                this.PolicyHandle = temphandle;

                List <string> privlist = NativeHelper.GetPrivilegeList();

                foreach (string priv in privlist)
                {
                    GetPrivilegeAndPrincipals(priv);
                }
            }

            else
            {
                string errmsg = null;

                if (NativeHelper.GetLastError(out errmsg) != 0)
                {
                    Messages.Add("\tLsaOpenPolicy: " + errmsg);
                }
            }
        }
        protected void GetPrivilegeAndPrincipals(string privName)
        {
            IntPtr enumbuffer = IntPtr.Zero;

            ulong cnt = 0;

            LSA_UNICODE_STRING lsapriv = NativeHelper.InitLSAString(privName);

            uint rc = Advapi32.LsaEnumerateAccountsWithUserRight(PolicyHandle, lsapriv, out enumbuffer, out cnt);

            if (rc == 0)
            {
                PrivilegeAndPrincipals privinfo = new PrivilegeAndPrincipals(privName);

                for (int step = 0; step < (int)cnt; step++)
                {
                    LSA_ENUMERATION_INFORMATION lsaenum = (LSA_ENUMERATION_INFORMATION)Marshal.PtrToStructure(enumbuffer, typeof(LSA_ENUMERATION_INFORMATION));

                    SidInfo sinfo = new SidInfo(lsaenum.Sid);

                    privinfo.Principals.AddSafe(sinfo);

                    privinfo.SidPrincipals.AddSafe(sinfo.Sid);

                    try
                    { enumbuffer = (IntPtr)((Int64)enumbuffer + Marshal.SizeOf(typeof(LSA_ENUMERATION_INFORMATION))); }

                    catch (Exception)
                    { break; }
                }

                this.Privileges.AddSafe(privinfo);
            }

            else
            {
                string errmsg = null;

                if (NativeHelper.GetLastError(out errmsg) != 0)
                {
                    Messages.Add("\tLsaEnumerateAccountsWithUserRight: " + errmsg);
                }
            }
        }
 internal static extern uint LsaEnumerateAccountsWithUserRight(IntPtr PolicyHandle,
                                                               LSA_UNICODE_STRING UserRights,
                                                               out IntPtr EnumerationBuffer,
                                                               out ulong CountReturned);
 internal static extern uint LsaOpenPolicy(LSA_UNICODE_STRING SystemName,
                                           ref LSA_OBJECT_ATTRIBUTES ObjectAttributes,
                                           LSA_POLICY_ACCESS AccessMask,
                                           out IntPtr PolicyHandle);