/// <summary> /// Write an array of bytes in the remote process. /// </summary> /// <param name="address">The address where the array is written.</param> /// <param name="byteArray">The array of bytes to write.</param> /// <param name="isRelative">[Optional] State if the address is relative to the main module.</param> protected void WriteBytes(IntPtr address, byte[] byteArray, bool isRelative = true) { // Change the protection of the memory to allow writable using (new MemoryProtection(this, isRelative ? MakeAbsolute(address) : address, MarshalType <byte> .Size * byteArray.Length)) { // Write the byte array MemoryCore.WriteBytes(Handle, isRelative ? MakeAbsolute(address) : address, byteArray); } }
/// <summary> /// Writes a set of bytes to memory. /// </summary> /// <param name="address">The address.</param> /// <param name="bytes">The bytes.</param> /// <param name="isRelative">if set to <c>true</c> [is relative].</param> /// <returns> /// Number of bytes written. /// </returns> public override int WriteBytes(IntPtr address, byte[] bytes, bool isRelative = false) { // Change the protection of the memory to allow writable using ( new MemoryProtection(this, isRelative ? GetAbsolute(address) : address, MarshalType <byte> .Size * bytes.Length)) { // Write the byte array MemoryCore.WriteBytes(Handle, isRelative ? GetAbsolute(address) : address, bytes); } return(bytes.Length); }
public void VirtualProtectExWriteReadBytes() { // Arrange var handle = MemoryCore.OpenProcess(ProcessAccessFlags.AllAccess, Resources.ProcessTest.Id); var expected = new byte[] { 0x90, 0x90, 0x90, 0x90, 0x90 }; var memory = new IntPtr(0x00400000); // Act try { MemoryCore.ChangeProtection(handle, memory, 5, MemoryProtectionFlags.ExecuteReadWrite); MemoryCore.WriteBytes(handle, memory, expected); var actual = MemoryCore.ReadBytes(handle, memory, 5); // Assert CollectionAssert.AreEqual(expected, actual, "The collections are not equal."); } catch (Win32Exception ex) { Assert.Fail(ex.Message); } }