Binarysharp.MemoryManagement.Memory.MemoryCore.ReadBytes C# (CSharp) Method

ReadBytes() public static method

Reads an array of bytes in the memory form the target process.
public static ReadBytes ( SafeMemoryHandle processHandle, IntPtr address, int size ) : byte[]
processHandle Binarysharp.MemoryManagement.Native.SafeMemoryHandle A handle to the process with memory that is being read.
address System.IntPtr A pointer to the base address in the specified process from which to read.
size int The number of bytes to be read from the specified process.
return byte[]
        public static byte[] ReadBytes(SafeMemoryHandle processHandle, IntPtr address, int size)
        {
            // Check if the handles are valid
            HandleManipulator.ValidateAsArgument(processHandle, "processHandle");
            HandleManipulator.ValidateAsArgument(address, "address");

            // Allocate the buffer
            var buffer = new byte[size];
            int nbBytesRead;

            // Read the data from the target process
            if (NativeMethods.ReadProcessMemory(processHandle, address, buffer, size, out nbBytesRead) && size == nbBytesRead)
                return buffer;

            // Else the data couldn't be read, throws an exception
            throw new Win32Exception(string.Format("Couldn't read {0} byte(s) from 0x{1}.", size, address.ToString("X")));
        }