public static extern bool AdjustTokenPrivileges( SafeNativeHandle TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges, SafeMemoryBuffer NewState, UInt32 BufferLength, SafeMemoryBuffer PreviousState, out UInt32 ReturnLength);
/// <summary> /// Get's the status of all the privileges on the token specified /// </summary> /// <param name="token">The process token to get the privilege status on</param> /// <returns>Dictionary where the key is the privilege constant and the value is the PrivilegeAttributes flags</returns> public static Dictionary <String, PrivilegeAttributes> GetAllPrivilegeInfo(SafeHandle token) { SafeNativeHandle hToken = null; if (!NativeMethods.OpenProcessToken(token, TokenAccessLevels.Query, out hToken)) { throw new Win32Exception("OpenProcessToken() failed"); } using (hToken) { UInt32 tokenLength = 0; NativeMethods.GetTokenInformation(hToken, TOKEN_PRIVILEGES, new SafeMemoryBuffer(0), 0, out tokenLength); NativeHelpers.LUID_AND_ATTRIBUTES[] privileges; using (SafeMemoryBuffer privilegesPtr = new SafeMemoryBuffer((int)tokenLength)) { if (!NativeMethods.GetTokenInformation(hToken, TOKEN_PRIVILEGES, privilegesPtr, tokenLength, out tokenLength)) { throw new Win32Exception("GetTokenInformation() for TOKEN_PRIVILEGES failed"); } NativeHelpers.TOKEN_PRIVILEGES privilegeInfo = (NativeHelpers.TOKEN_PRIVILEGES)Marshal.PtrToStructure( privilegesPtr.DangerousGetHandle(), typeof(NativeHelpers.TOKEN_PRIVILEGES)); privileges = new NativeHelpers.LUID_AND_ATTRIBUTES[privilegeInfo.PrivilegeCount]; PtrToStructureArray(privileges, IntPtr.Add(privilegesPtr.DangerousGetHandle(), Marshal.SizeOf(privilegeInfo.PrivilegeCount))); } return(privileges.ToDictionary(p => GetPrivilegeName(p.Luid), p => p.Attributes)); } }
public static extern bool OpenProcessToken( SafeHandle ProcessHandle, TokenAccessLevels DesiredAccess, out SafeNativeHandle TokenHandle);
public static extern bool GetTokenInformation( SafeNativeHandle TokenHandle, UInt32 TokenInformationClass, SafeMemoryBuffer TokenInformation, UInt32 TokenInformationLength, out UInt32 ReturnLength);