/// <summary> /// Remotely releases virtual memory. Throws Win32Exception if it fails. /// </summary> /// <param name="address">The absolute memory address to free</param> /// <param name="allocationType">The allocation Type</param> public void Release(IntPtr address, Kernel32.AllocationType allocationType = Kernel32.AllocationType.MemRelease) { IntPtr targetProcess = Process.Handle.GetAccess(Kernel32.ProcessSecurity.ProcessVmOperation); if (!Kernel32.VirtualFreeEx(targetProcess, address, UIntPtr.Zero, allocationType)) { throw new Win32Exception(); } }
/// <summary> /// Remotely allocates memory. Throws Win32Exception if it fails. /// </summary> /// <param name="size">The size in bytes to allocate</param> /// <param name="allocationType"></param> /// <param name="protect"></param> /// <returns>The absolute memory address of the allocated memory</returns> public IntPtr Allocate(int size, Kernel32.AllocationType allocationType = Kernel32.AllocationType.MemCommit | Kernel32.AllocationType.MemReserve, Kernel32.Protect protect = Kernel32.Protect.PageExecuteReadwrite) { IntPtr targetProcess = Process.Handle.GetAccess(Kernel32.ProcessSecurity.ProcessVmOperation); IntPtr processMemory = Kernel32.VirtualAllocEx(targetProcess, IntPtr.Zero, (UIntPtr)size, allocationType, protect); if (processMemory == IntPtr.Zero) { throw new Win32Exception(); } return(processMemory); }