Пример #1
0
        private static AdjustPrivilegeResult AdjustPrivilege(
            AccessTokenHandle accessTokenHandle,
            Luid luid,
            PrivilegeAttributes privilegeAttributes)
        {
            TokenPrivilege newState = new TokenPrivilege
            {
                PrivilegeCount = 1,
                Privilege = new LuidAndAttributes
                {
                    Attributes = privilegeAttributes,
                    Luid = luid
                }
            };
            TokenPrivilege previousState = new TokenPrivilege();
            int returnLength = 0;

            if (!NativeMethods.AdjustTokenPrivileges(
                accessTokenHandle,
                false,
                ref newState,
                Marshal.SizeOf(previousState),
                ref previousState,
                ref returnLength))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return (AdjustPrivilegeResult)previousState.PrivilegeCount;
        }
Пример #2
0
        private static string GetPrivilegeName(Luid luid)
        {
            StringBuilder nameBuilder = new StringBuilder();
            int nameLength = 0;
            if (NativeMethods.LookupPrivilegeName(String.Empty, ref luid, nameBuilder, ref nameLength))
            {
                return String.Empty;
            }

            int lastWin32Error = Marshal.GetLastWin32Error();
            if (lastWin32Error != NativeMethods.ErrorInsufficientBuffer)
            {
                throw new Win32Exception(lastWin32Error);
            }

            nameBuilder.EnsureCapacity(nameLength);
            if (!NativeMethods.LookupPrivilegeName(String.Empty, ref luid, nameBuilder, ref nameLength))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return nameBuilder.ToString();
        }
Пример #3
0
        private static Luid GetLuid(Privilege privilege)
        {
            if (LuidDictionary.ContainsKey(privilege))
            {
                return LuidDictionary[privilege];
            }

            Luid luid = new Luid();
            if (!NativeMethods.LookupPrivilegeValue(String.Empty, PrivilegeConstantsDictionary[privilege], ref luid))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            LuidDictionary.Add(privilege, luid);
            return luid;
        }