Пример #1
0
 public static extern bool AdjustTokenPrivileges(
     IntPtr tokenHandle,
     [MarshalAs(UnmanagedType.Bool)]bool disableAllPrivileges,
     ref TokenPrivileges newState,
     UInt32 pOldStateMaxLength,
     IntPtr pOldState,
     IntPtr pOldStateLength
 );
Пример #2
0
        public static void AdjustProcessPrivilege(int processId, string privilegeName, bool privilegeStatus)
        {
            UInt64 luid;
            if (!LookupPrivilegeValue(null, privilegeName, out luid)) {
                var error = GetLastError();
                throw new Exception(String.Format("LookupPrivilegeValue failed: Error {0:x8}", error));
            }

            using (var handle = OpenProcessHandle(ProcessAccessFlags.QueryInformation, false, processId)) {
                IntPtr token;
                if (!OpenProcessToken(handle.DangerousGetHandle(), TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query, out token)) {
                    var error = GetLastError();
                    throw new Exception(String.Format("OpenProcessToken failed: Error {0:x8}", error));
                }
                try {
                    var newState = new TokenPrivileges {
                        PrivilegeCount = 1,
                        Luid = luid,
                        Attributes = privilegeStatus ? PrivilegeAttributes.Enabled : PrivilegeAttributes.Removed
                    };
                    if (!AdjustTokenPrivileges(token, false, ref newState, 0, IntPtr.Zero, IntPtr.Zero)) {
                        var error = GetLastError();
                        throw new Exception(String.Format("AdjustTokenPrivileges failed: Error {0:x8}", error));
                    }
                } finally {
                    CloseHandle(token);
                }
            }
        }