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

WriteBytes() public static method

Writes data to an area of memory in a specified process.
public static WriteBytes ( SafeMemoryHandle processHandle, IntPtr address, byte byteArray ) : int
processHandle Binarysharp.MemoryManagement.Native.SafeMemoryHandle A handle to the process memory to be modified.
address System.IntPtr A pointer to the base address in the specified process to which data is written.
byteArray byte A buffer that contains data to be written in the address space of the specified process.
return int
        public static int WriteBytes(SafeMemoryHandle processHandle, IntPtr address, byte[] byteArray)
        {
            // Check if the handles are valid
            HandleManipulator.ValidateAsArgument(processHandle, "processHandle");
            HandleManipulator.ValidateAsArgument(address, "address");

            // Create the variable storing the number of bytes written
            int nbBytesWritten;

            // Write the data to the target process
            if (NativeMethods.WriteProcessMemory(processHandle, address, byteArray, byteArray.Length, out nbBytesWritten))
            {
                // Check whether the length of the data written is equal to the inital array
                if (nbBytesWritten == byteArray.Length)
                    return nbBytesWritten;
            }

            // Else the data couldn't be written, throws an exception
            throw new Win32Exception(string.Format("Couldn't write {0} bytes to 0x{1}", byteArray.Length, address.ToString("X")));
        }