6

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

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

我试过修改代码来解决这个问题,但没有任何效果,我很困惑。

关于为什么在 32 位系统上失败的任何想法?它在 Windows Vista 和 7 32 位上都失败了,所以这是一个 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);
4

1 回答 1

4

已修复,只需在 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);
于 2012-11-29T03:55:07.127 回答