public static MemoryProtectionType ChangeMemoryProtection(SafeMemoryHandle processHandle, IntPtr address, int size, MemoryProtectionType newProtect = MemoryProtectionType.PAGE_EXECUTE_READWRITE) { MemoryProtectionType oldProtect; if (!Imports.VirtualProtectEx(processHandle, address, size, newProtect, out oldProtect)) { throw new Win32Exception( $"[Error Code: {Marshal.GetLastWin32Error()}] Unable to change memory protection of process handle 0x{processHandle.DangerousGetHandle().ToString("X")} at 0x{address.ToString($"X{IntPtr.Size}")}[Size: {size}] to {newProtect.ToString("X")}"); } return(oldProtect); }
public static MemoryProtectionType ChangeMemoryProtection(IntPtr address, int size, MemoryProtectionType newProtect = MemoryProtectionType.PAGE_EXECUTE_READWRITE) { MemoryProtectionType oldProtect; if (!Imports.VirtualProtect(address, size, newProtect, out oldProtect)) { throw new Win32Exception($"[Win32 Error: {Marshal.GetLastWin32Error()}] " + $"Unable to change memory protection at " + $"0x{address.ToString($"X{IntPtr.Size}")}[Size: {size}] to {newProtect.ToString("X")}"); } return(oldProtect); }