Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/MIDebugEngine/AD7.Impl/AD7Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,27 @@ public int ReadAt(IDebugMemoryContext2 pStartContext, uint dwCount, byte[] rgbMe

public int WriteAt(IDebugMemoryContext2 pStartContext, uint dwCount, byte[] rgbMemory)
{
throw new NotImplementedException();
try
{
if (!(pStartContext is AD7MemoryAddress memContext))
return Constants.E_INVALIDARG;

ulong address = memContext.Address;
bool isWriteSuccess = false;
DebuggedProcess.WorkerThread.RunOperation(async () =>
{
isWriteSuccess = await DebuggedProcess.WriteProcessMemory(address, dwCount, rgbMemory);
});

if (!isWriteSuccess)
return Constants.E_FAIL;

return Constants.S_OK;
}
catch
{
return Constants.E_FAIL;
}
}

#endregion
Expand Down
23 changes: 23 additions & 0 deletions src/MIDebugEngine/Engine.Impl/DebuggedProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2119,6 +2119,29 @@ internal async Task<uint> ReadProcessMemory(ulong address, uint count, byte[] by
return toRead;
}

internal async Task<bool> WriteProcessMemory(ulong address, uint dwCount, byte[] rgbMemory)
{
try
{
// Convert bytes to hex string
StringBuilder hex = new StringBuilder((int)dwCount * 2);
for (int i = 0; i < dwCount; i++)
hex.AppendFormat(CultureInfo.InvariantCulture, "{0:x2}", rgbMemory[i]);

// Send the MI command
string cmd = $"-data-write-memory-bytes {EngineUtils.AsAddr(address, this.Is64BitArch)} {hex}";
var result = await this.CmdAsync(cmd, ResultClass.None);

// Check result
return result.ResultClass != ResultClass.error;
}
catch (MIException ex)
{
Logger.WriteLine(LogLevel.Warning, $"WriteProcessMemory failed: {ex.Message}");
return false;
}
}

internal async Task<Tuple<ulong, ulong>> FindValidMemoryRange(ulong address, uint count, int offset)
{
var ret = new Tuple<ulong, ulong>(0, 0); // init to an empty range
Expand Down
1 change: 1 addition & 0 deletions src/MIDebugEngine/Engine.Impl/Structures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class Constants
public const int E_NOTIMPL = unchecked((int)0x80004001);
public const int E_FAIL = unchecked((int)0x80004005);
public const int E_ABORT = unchecked((int)(0x80004004));
public const int E_INVALIDARG = unchecked((int)(0x80070057));
public const int RPC_E_SERVERFAULT = unchecked((int)(0x80010105));
};

Expand Down