public static bool ChangeProcessPrivilege(Process process, string privilege, bool enable) { SafeTokenHandle handle; if (!OpenProcessToken(process.Handle, TOKEN.TOKEN_ADJUST_PRIVILEGES | TOKEN.TOKEN_QUERY, out handle)) { return false; } try { LUID lpLuid = new LUID(); if (!LookupPrivilegeValue(null, privilege, out lpLuid)) { return false; } TOKEN_PRIVILEGES newState = new TOKEN_PRIVILEGES { PrivilegeCount = 1, Privileges = new LUID_AND_ATTRIBUTES[1] }; newState.Privileges[0].Luid = lpLuid; newState.Privileges[0].Attributes = enable ? 2 : 0; if (!AdjustTokenPrivileges(handle, false, ref newState, (uint) Marshal.SizeOf(newState), IntPtr.Zero, IntPtr.Zero)) { return false; } } finally { handle.Close(); } return true; }
public static extern bool AdjustTokenPrivileges(SafeTokenHandle TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges, [In] ref TOKEN_PRIVILEGES NewState, uint BufferLength, out TOKEN_PRIVILEGES PreviousState, out uint ReturnLength);