I am trying to write UEFI properties. it appears that i need to raise the token privileges. so i have used http://www.pinvoke.net/default.aspx/advapi32.adjusttokenprivileges as a guide to do this.
I keep getting the error E_Invalid Arguments
public static void EnableDisablePrivilege(string PrivilegeName, bool EnableDisable)
{
//Gets the process token handle using pinvoke
var inTokenHandle = IntPtr.Zero;
if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query, out inTokenHandle))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
return;
}
//Enumerates the current privileges of the token handle
var inNewState = new TOKEN_PRIVILEGES { PrivilegeCount = 1, Privileges = new LUID_AND_ATTRIBUTES[1] };
//Attempts to lookup the inputed privilege
LUID luid;
if (!LookupPrivilegeValue(null, PrivilegeName, out luid))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
return;
}
//Adds the new privilige to the token whilst retaining all the other priviliges
inNewState.Privileges[0].LUID = luid;
inNewState.Privileges[0].Attributes = (uint)(EnableDisable ? 2 : 0);
//Attempts to set the priviliges to the token handle.
if (!AdjustTokenPrivileges(inTokenHandle, false, ref inNewState, 256, IntPtr.Zero, IntPtr.Zero))
{
Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
return;
}
}
/// <summary>
/// An LUID is a 64-bit value guaranteed to be unique only on the system on which it was generated
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct LUID
{
/// <summary>
/// Low Order BIts
/// </summary>
public uint LowPart { get; set; }
/// <summary>
/// High Order Bits
/// </summary>
public int HighPart { get; set; }
}
/// <summary>
/// The LUID_AND_ATTRIBUTES structure represents a locally unique identifier (LUID) and its attributes.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct LUID_AND_ATTRIBUTES
{
/// <summary>
/// Specifies the HIgh Order BIts and Low Order Bits Stucture
/// </summary>
public LUID LUID { get; set; }
/// <summary>
/// Specifies attributes of the LUID. This value contains up to 32 one-bit flags.
/// </summary>
public uint Attributes { get; set; }
}
/// <summary>
/// The TOKEN_PRIVILEGES structure contains information about a set of privileges for an access token.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_PRIVILEGES
{
/// <summary>
/// This must be set to the number of entries in the Privileges array.
/// </summary>
public uint PrivilegeCount { get; set; }
/// <summary>
/// Specifies an array of LUID_AND_ATTRIBUTES structures.
/// </summary>
public LUID_AND_ATTRIBUTES[] Privileges { get; set; }
}
[DllImport("advapi32", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool OpenProcessToken(
IntPtr ProcessHandle,
TokenAccessLevels DesiredAccess,
out IntPtr TokenHandle);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AdjustTokenPrivileges(
IntPtr TokenHandle,
[MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges,
ref TOKEN_PRIVILEGES NewState,
uint BufferLengthInBytes,
IntPtr PreviousState,
IntPtr ReturnLength);
[DllImport("advapi32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool LookupPrivilegeValue(
string lpSystemName,
string lpName,
out LUID lpLuid);
Calling Code:
//Enable a privilege by implementing the following line in your code:
Privileges.EnableDisablePrivilege("SeSystemEnvironmentPrivilege", true);
Im honestly not sure where i have gone wrong here.
User contributions licensed under CC BY-SA 3.0