/// <summary> /// Allocates memory in remote process /// </summary> /// <param name="lpAddress">The address to allocate at (IntPtr.Zero for any)</param> /// <param name="dwSize">The size of memory to allocate</param> /// <param name="flAllocationType">The allocation type</param> /// <param name="flProtect">The memory protection</param> /// <returns>The address of allocated memory (IntPtr.Zero on fail)</returns> /// <exception cref="MemoryAllocationException">Thrown if memory can't be allocated</exception> public IntPtr Allocate(IntPtr lpAddress, uint dwSize, Native.AllocationType flAllocationType = Native.AllocationType.Commit | Native.AllocationType.Reserve, Native.MemoryProtection flProtect = Native.MemoryProtection.ExecuteReadWrite) { if (_handle == IntPtr.Zero) { throw new InvalidOperationException("Requires open handle."); } IntPtr allocation = Native.VirtualAllocEx(_handle, lpAddress, dwSize, flAllocationType, flProtect); if (allocation == IntPtr.Zero) { throw new MemoryAllocationException(); } return(allocation); }
public static ulong AllocateMemory(this System.Diagnostics.Process process, uint length, Native.AllocationType allocationType, Native.MemoryProtection memoryProtection) => Native.VirtualAllocEx(process.Handle, 0, length, allocationType, memoryProtection);
public static ulong AllocateAndWrite(this System.Diagnostics.Process process, byte[] buffer, Native.AllocationType allocationType, Native.MemoryProtection memoryProtection) { ulong allocatedMemory = process.AllocateMemory((uint)buffer.Length, allocationType, memoryProtection); process.WriteRawMemory(buffer, allocatedMemory); return(allocatedMemory); }