예제 #1
0
파일: Win32.cs 프로젝트: kg/shootblues
 public static extern bool AdjustTokenPrivileges(
     IntPtr tokenHandle,
     [MarshalAs(UnmanagedType.Bool)]bool disableAllPrivileges,
     ref TokenPrivileges newState,
     UInt32 pOldStateMaxLength,
     IntPtr pOldState,
     IntPtr pOldStateLength
 );
예제 #2
0
파일: Win32.cs 프로젝트: kg/shootblues
        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);
                }
            }
        }