public static bool MySetPrivilege(string sPrivilege, bool enablePrivilege) { Trust.TOKEN_PRIVILEGES newState = new Trust.TOKEN_PRIVILEGES(); Trust.TOKEN_PRIVILEGES previousState = new Trust.TOKEN_PRIVILEGES(); Trust.LUID luid = new Trust.LUID(); int returnLength = 0; IntPtr zero = IntPtr.Zero; if (!Trust.OpenProcessToken(Trust.GetCurrentProcess(), Trust.TokenAccessRights.AllAccess, ref zero) || !Trust.LookupPrivilegeValue((string)null, sPrivilege, ref luid)) { return(false); } newState.PrivilegeCount = 1; newState.Privileges = new Trust.LUID_AND_ATTRIBUTES[64]; newState.Privileges[0].Luid = luid; newState.Privileges[0].Attributes = !enablePrivilege ? 0 : 2; previousState.PrivilegeCount = 64; previousState.Privileges = new Trust.LUID_AND_ATTRIBUTES[64]; if (Trust.AdjustTokenPrivileges(zero, false, ref newState, 16, ref previousState, ref returnLength)) { return(true); } Trust.GetLastError(); return(false); }
internal static extern bool AdjustTokenPrivileges( [In] IntPtr accessTokenHandle, [MarshalAs(UnmanagedType.Bool), In] bool disableAllPrivileges, [In] ref Trust.TOKEN_PRIVILEGES newState, [In] int bufferLength, [In, Out] ref Trust.TOKEN_PRIVILEGES previousState, [In, Out] ref int returnLength);