C# - AdjustTokenPrivileges not working on 32bit

6

I am trying to obtain a privilege in my .NET 4 C# application. This code works and the privilege is obtained successfully, but only on 64bit systems. When the same code is run on a 32bit system, the code fails at AdjustTokenPrivileges with this exception:

Invalid access to memory location. (Exception from HRESULT: 0x800703E6)

I've tried modifying the code to fix the issue, but nothing has been working and I am pretty stumped.

Any ideas on why this fails on 32bit systems? It fails on both Windows Vista and 7 32bit, so it is a 32bit specific problem.

The method:

        public static void EnableDisablePrivilege(string PrivilegeName, bool EnableDisable)
    {
        var htok = IntPtr.Zero;
        if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TokenAccessLevels.AdjustPrivileges | TokenAccessLevels.Query, out htok))
        {
            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            return;
        }
        var tkp = new TOKEN_PRIVILEGES { PrivilegeCount = 1, Privileges = new LUID_AND_ATTRIBUTES[1] };
        LUID luid;
        if (!LookupPrivilegeValue(null, PrivilegeName, out luid))
        {
            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            return;
        }
        tkp.Privileges[0].LUID = luid;
        tkp.Privileges[0].Attributes = (uint)(EnableDisable ? 2 : 0);
        TOKEN_PRIVILEGES prv;
        uint rb;
        if (!AdjustTokenPrivileges(htok, false, tkp, 256, out prv, out rb))
        {
            Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            return;
        }
    }

Implementation:

EnableDisablePrivilege("SeManageVolumePrivilege", true);

PInvoke declarations:

        [StructLayout(LayoutKind.Sequential)]
    public struct LUID
    {
        private uint lp;
        private int hp;

        public uint LowPart
        {
            get { return lp; }
            set { lp = value; }
        }

        public int HighPart
        {
            get { return hp; }
            set { hp = value; }
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct LUID_AND_ATTRIBUTES
    {
        private LUID luid;
        private uint attributes;

        public LUID LUID
        {
            get { return luid; }
            set { luid = value; }
        }

        public uint Attributes
        {
            get { return attributes; }
            set { attributes = value; }
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct TOKEN_PRIVILEGES
    {
        private uint prvct;
        [MarshalAs(UnmanagedType.SafeArray, SizeConst = 1)]
        private LUID_AND_ATTRIBUTES[] privileges;

        public uint PrivilegeCount
        {
            get { return prvct; }
            set { prvct = value; }
        }

        public LUID_AND_ATTRIBUTES[] Privileges
        {
            get { return privileges; }
            set { privileges = value; }
        }
    }

    [DllImport("advapi32", SetLastError = true)]
    public static extern bool OpenProcessToken(IntPtr ProcessHandle, TokenAccessLevels DesiredAccess, out IntPtr TokenHandle);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAllPrivileges, TOKEN_PRIVILEGES NewState, uint BufferLength, out TOKEN_PRIVILEGES PreviousState, out uint ReturnLength);

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LookupPrivilegeValue(string lpSystemName, string lpName, out LUID lpLuid);
c#
.net
pinvoke
32bit-64bit
privileges
asked on Stack Overflow Nov 28, 2012 by Eaton

1 Answer

4

Fixed, just had to put a ref before NewState:

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, uint Bufferlength, IntPtr PreviousState, IntPtr ReturnLength);
answered on Stack Overflow Nov 29, 2012 by Eaton

User contributions licensed under CC BY-SA 3.0