/// <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); }
/// <summary> /// Releases the memory used by the region. /// </summary> public void Release() { // Release the memory MemoryCore.Free(MemorySharp.Handle, BaseAddress); // Remove the pointer BaseAddress = IntPtr.Zero; }
/// <summary> /// Initializes a new instance of the <see cref="RemoteAllocation"/> class. /// </summary> /// <param name="memorySharp">The reference of the <see cref="MemorySharp"/> object.</param> /// <param name="size">The size of the allocated memory.</param> /// <param name="protection">The protection of the allocated memory.</param> /// <param name="mustBeDisposed">The allocated memory will be released when the finalizer collects the object.</param> internal RemoteAllocation(MemorySharp memorySharp, int size, MemoryProtectionFlags protection = MemoryProtectionFlags.ExecuteReadWrite, bool mustBeDisposed = true) : base(memorySharp, MemoryCore.Allocate(memorySharp.Handle, size, protection)) { // Set local vars MustBeDisposed = mustBeDisposed; IsDisposed = false; }
/// <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, int 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); }