C# - AdjustTokenPrivileges不能在32位上运行

时间:2012-11-28 23:32:11

标签: c# .net pinvoke 32bit-64bit privileges

我正在尝试在.NET 4 C#应用程序中获得权限。此代码有效,并且成功获得了权限,但仅限于64位系统。当在32位系统上运行相同的代码时,代码在AdjustTokenPrivileges上失败,但有以下异常:

对内存位置的无效访问。 (HRESULT异常:0x800703E6)

我已经尝试修改代码来修复问题,但没有任何工作,我很难过。

有关为何在32位系统上失败的任何想法?它在Windows Vista和7 32bit上都失败了,因此它是一个32位的特定问题。

方法:

        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;
        }
    }

实现:

EnableDisablePrivilege("SeManageVolumePrivilege", true);

PInvoke声明:

        [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);

1 个答案:

答案 0 :(得分:2)

修正了,只需在NewState之前输入一个ref:

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