Beispiel #1
0
 /// <summary>
 /// Restores the initial protection of the memory.
 /// </summary>
 public virtual void Dispose()
 {
     // Restore the memory protection
     MemoryCore.ChangeProtection(_memorySharp.Handle, BaseAddress, Size, OldProtection);
     // Avoid the finalizer
     GC.SuppressFinalize(this);
 }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemoryProtection"/> class.
        /// </summary>
        /// <param name="memorySharp">The reference of the <see cref="MemorySharp"/> 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(MemorySharp memorySharp, IntPtr baseAddress, IntPtr size, MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite,
                                bool mustBeDisposed = true)
        {
            // Save the parameters
            _memorySharp   = memorySharp;
            BaseAddress    = baseAddress;
            NewProtection  = protection;
            Size           = size;
            MustBeDisposed = mustBeDisposed;

            // Change the memory protection
            OldProtection = MemoryCore.ChangeProtection(_memorySharp.Handle, baseAddress, size, protection);
        }