implement r/w of process memory

This commit is contained in:
2024-12-30 18:06:33 +00:00
parent ff7674615d
commit d6fb43adc1
6 changed files with 103 additions and 10 deletions
+51
View File
@@ -0,0 +1,51 @@
use super::handle::Handle;
use std::ffi::c_void;
use windows::Win32::System::Diagnostics::Debug::{ReadProcessMemory, WriteProcessMemory};
pub fn read_process_memory<T>(
handle: &Handle,
address: usize,
value: &mut T,
) -> Result<(), crate::Error> {
unsafe {
match ReadProcessMemory(
handle.0,
address as *const c_void,
value as *mut T as *mut c_void,
size_of::<T>(),
None,
) {
Ok(()) => return Ok(()),
Err(why) => {
return Err(crate::Error::MemoryError(format!(
"failed to read memory: {why}"
)));
}
};
}
}
pub fn write_process_memory<T>(
handle: &Handle,
address: usize,
value: &mut T,
) -> Result<(), crate::Error> {
unsafe {
match WriteProcessMemory(
handle.0,
address as *const c_void,
value as *mut T as *mut c_void,
size_of::<T>(),
None,
) {
Ok(()) => return Ok(()),
Err(why) => {
return Err(crate::Error::MemoryError(format!(
"failed to write memory: {why}"
)));
}
};
}
}