/// <summary> /// Restores the initial protection of the memory. /// </summary> public virtual void Dispose() { // Restore the memory protection MemoryHelper.ChangeProtection(Handle, BaseAddress, Size, OldProtection); // Avoid the finalizer GC.SuppressFinalize(this); }
/// <summary> /// Initializes a new instance of the <see cref="MemoryProtection" /> class. /// </summary> /// <param name="handle">The reference of the <see cref="SafeMemoryHandle" /> object.</param> /// <param name="baseAddress">The base address of the memory to change the protection.</param> /// <param name="size">The size of the memory to change.</param> /// <param name="protection">The new protection to apply.</param> /// <param name="mustBeDisposed">The resource will be automatically disposed when the finalizer collects the object.</param> public MemoryProtection(SafeMemoryHandle handle, IntPtr baseAddress, int size, MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite, bool mustBeDisposed = true) { // Save the parameters Handle = handle; BaseAddress = baseAddress; NewProtection = protection; Size = size; MustBeDisposed = mustBeDisposed; // Change the memory protection OldProtection = MemoryHelper.ChangeProtection(Handle, baseAddress, size, protection); }