Binarysharp.MemoryManagement.Memory.MemoryCore.ChangeProtection C# (CSharp) Méthode

ChangeProtection() public static méthode

Changes the protection on a region of committed pages in the virtual address space of a specified process.
public static ChangeProtection ( SafeMemoryHandle processHandle, IntPtr address, int size, MemoryProtectionFlags protection ) : MemoryProtectionFlags
processHandle Binarysharp.MemoryManagement.Native.SafeMemoryHandle A handle to the process whose memory protection is to be changed.
address System.IntPtr A pointer to the base address of the region of pages whose access protection attributes are to be changed.
size int The size of the region whose access protection attributes are changed, in bytes.
protection MemoryProtectionFlags The memory protection option.
Résultat MemoryProtectionFlags
        public static MemoryProtectionFlags ChangeProtection(SafeMemoryHandle processHandle, IntPtr address, int size, MemoryProtectionFlags protection)
        {
            // Check if the handles are valid
            HandleManipulator.ValidateAsArgument(processHandle, "processHandle");
            HandleManipulator.ValidateAsArgument(address, "address");

            // Create the variable storing the old protection of the memory page
            MemoryProtectionFlags oldProtection;

            // Change the protection in the target process
            if(NativeMethods.VirtualProtectEx(processHandle, address, size, protection, out oldProtection))
            {
                // Return the old protection
                return oldProtection;
            }

            // Else the protection couldn't be changed, throws an exception
            throw new Win32Exception(string.Format("Couldn't change the protection of the memory at 0x{0} of {1} byte(s) to {2}.", address.ToString("X"), size, protection));
        }

Usage Example

Exemple #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);
 }
All Usage Examples Of Binarysharp.MemoryManagement.Memory.MemoryCore::ChangeProtection