public static bool SetPrivilege(string privilege, bool enabled) { try { TokPriv1Luid tp = new TokPriv1Luid(); IntPtr hproc = System.Diagnostics.Process.GetCurrentProcess().Handle; IntPtr htok = IntPtr.Zero; if (!Win32API.OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok)) return false; if (!Win32API.LookupPrivilegeValue(null, privilege, ref tp.Luid)) return false; tp.Count = 1; tp.Luid = 0; tp.Attr = ((enabled) ? (SE_PRIVILEGE_ENABLED) : (0)); Win32API.AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero); if (Marshal.GetLastWin32Error() != 0) return false; return true; } catch { return false; } }
public static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall, ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);