Exemplo n.º 1
0
        public IntPtr Allocate(IntPtr preferred, long size, Native.MemoryProtection protectionFlags)
        {
            IntPtr allocatedMemory;

            if ((allocatedMemory = AllocateInternal(preferred, size, protectionFlags)) == IntPtr.Zero &&
                preferred != IntPtr.Zero)
            {
                return(Allocate(IntPtr.Zero, size, protectionFlags));
            }
            return(allocatedMemory);
        }
Exemplo n.º 2
0
 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);
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        /// <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);
        }
Exemplo n.º 5
0
 private IntPtr AllocateInternal(IntPtr preferred, long size,
                                 Native.MemoryProtection protectionFlags)
 {
     return(Native.VirtualAllocEx(ProcessHandle, preferred, (UIntPtr)size,
                                  Native.AllocationType.Commit | Native.AllocationType.Reserve, protectionFlags));
 }