BananaHook.Infrastructure.MemoryPageProtector.ExecuteWithProtection C# (CSharp) Method

ExecuteWithProtection() public method

public ExecuteWithProtection ( MemoryProtectionConstraints requestedProtection, System.Action action ) : void
requestedProtection MemoryProtectionConstraints
action System.Action
return 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

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