private static AdjustPrivilegeResult AdjustPrivilege(
            AccessTokenHandle accessTokenHandle,
            Luid luid,
            PrivilegeAttributes privilegeAttributes)
        {
            TokenPrivilege newState = new TokenPrivilege
            {
                PrivilegeCount = 1,
                Privilege = new LuidAndAttributes
                {
                    Attributes = privilegeAttributes,
                    Luid = luid
                }
            };
            TokenPrivilege previousState = new TokenPrivilege();
            int returnLength = 0;

            if (!NativeMethods.AdjustTokenPrivileges(
                accessTokenHandle,
                false,
                ref newState,
                Marshal.SizeOf(previousState),
                ref previousState,
                ref returnLength))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return (AdjustPrivilegeResult)previousState.PrivilegeCount;
        }
示例#2
0
 internal static extern bool AdjustTokenPrivileges(
     [In] AccessTokenHandle accessTokenHandle,
     [In, MarshalAs(UnmanagedType.Bool)] bool disableAllPrivileges,
     [In] ref TokenPrivilege newState,
     [In] int bufferLength,
     [In, Out] ref TokenPrivilege previousState,
     [In, Out] ref int returnLength);
        private static AdjustPrivilegeResult AdjustPrivilege(
            AccessTokenHandle accessTokenHandle,
            Luid luid,
            PrivilegeAttributes privilegeAttributes)
        {
            TokenPrivilege newState = new TokenPrivilege
            {
                PrivilegeCount = 1,
                Privilege      = new LuidAndAttributes
                {
                    Attributes = privilegeAttributes,
                    Luid       = luid
                }
            };
            TokenPrivilege previousState = new TokenPrivilege();
            int            returnLength  = 0;

            if (!NativeMethods.AdjustTokenPrivileges(
                    accessTokenHandle,
                    false,
                    ref newState,
                    Marshal.SizeOf(previousState),
                    ref previousState,
                    ref returnLength))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error());
            }

            return((AdjustPrivilegeResult)previousState.PrivilegeCount);
        }