Exemplo n.º 1
0
        public static void AdjustTokenPrivileges(SafeTokenHandle tokenHandle, bool disableAllPrivileges, ref TokenPrivileges newState,
                                                 ref TokenPrivileges previousState)
        {
            Contract.Requires <ArgumentNullException>(tokenHandle != null, "tokenHandle cannot be null");

            uint temp;

            if (!AdjustTokenPrivileges(tokenHandle.DangerousGetHandle(),
                                       disableAllPrivileges, ref newState,
                                       (uint)Marshal.SizeOf(previousState), ref previousState, out temp))
            {
                WindowsApi.NativeMethods.ReportWin32Exception();
            }

            var error = Marshal.GetLastWin32Error();

            if (error == ErrorCodes.NotAllAssigned)
            {
                try {
                    throw new PrivilegeNotHeldException(GetPrivilegeName(PrivilegeFromLuid(newState.Privileges[0].Luid)));
                }
                catch (InvalidOperationException) {
                    throw new PrivilegeNotHeldException();
                }
            }
        }