BananaHook.Infrastructure.MemoryPageProtector.ExecuteWithProtection C# (CSharp) Méthode

ExecuteWithProtection() public méthode

public ExecuteWithProtection ( MemoryProtectionConstraints requestedProtection, System.Action action ) : void
requestedProtection MemoryProtectionConstraints
action System.Action
Résultat void
        public void ExecuteWithProtection(MemoryProtectionConstraints requestedProtection, Action action)
        {
            MemoryProtectionConstraints oldProtection = MemoryProtectionConstraints.None;

            Helper.ThrowWin32ExceptionIfFalse(() =>
                _protection.VirtualProtect(_address, _length, requestedProtection, out oldProtection));

            try
            {
                action();
            }
            finally
            {
                Helper.ThrowWin32ExceptionIfFalse(() =>
                    _protection.VirtualProtect(_address, _length, oldProtection, out oldProtection));
            }
        }

Usage Example

Exemple #1
0
        public void Apply()
        {
            if (IsApplied)
            {
                return;
            }

            _protector.ExecuteWithProtection(MemoryProtectionConstraints.PAGE_EXECUTE_READWRITE, () =>
            {
                _original = _memory.ReadBytes(TargetAddress, _replaceWith.Length);
                _memory.WriteBytes(TargetAddress, _replaceWith);
            });
            IsApplied = true;
        }