improve error handling syntax
This commit is contained in:
+4
-12
@@ -5,7 +5,7 @@ use std::{ffi::c_void, ptr};
|
||||
use windows::Win32::System::Diagnostics::Debug::{ReadProcessMemory, WriteProcessMemory};
|
||||
|
||||
pub fn read<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), crate::Error> {
|
||||
match unsafe {
|
||||
unsafe {
|
||||
ReadProcessMemory(
|
||||
handle.0,
|
||||
address as *const c_void,
|
||||
@@ -13,16 +13,12 @@ pub fn read<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), cra
|
||||
size_of::<T>(),
|
||||
None,
|
||||
)
|
||||
} {
|
||||
Ok(()) => Ok(()),
|
||||
Err(why) => Err(crate::Error::AccessMemoryError(format!(
|
||||
"failed to read memory: {why}"
|
||||
))),
|
||||
}
|
||||
.map_err(|why| crate::Error::AccessMemoryError(format!("failed to read memory: {why}")))
|
||||
}
|
||||
|
||||
pub fn write<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), crate::Error> {
|
||||
match unsafe {
|
||||
unsafe {
|
||||
WriteProcessMemory(
|
||||
handle.0,
|
||||
address as *const c_void,
|
||||
@@ -30,10 +26,6 @@ pub fn write<T>(handle: &Handle, address: usize, value: &mut T) -> Result<(), cr
|
||||
size_of::<T>(),
|
||||
None,
|
||||
)
|
||||
} {
|
||||
Ok(()) => Ok(()),
|
||||
Err(why) => Err(crate::Error::AccessMemoryError(format!(
|
||||
"failed to write memory: {why}"
|
||||
))),
|
||||
}
|
||||
.map_err(|why| crate::Error::AccessMemoryError(format!("failed to write memory: {why}")))
|
||||
}
|
||||
|
||||
+18
-29
@@ -13,26 +13,21 @@ pub struct ProcessSnapshot {
|
||||
|
||||
impl ProcessSnapshot {
|
||||
pub fn get_processes() -> Result<Vec<Self>, crate::Error> {
|
||||
let snapshot = match unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) } {
|
||||
Ok(snapshot) => snapshot,
|
||||
Err(why) => {
|
||||
return Err(crate::Error::CreateSnapshotError(format!(
|
||||
let snapshot =
|
||||
unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) }.map_err(|why| {
|
||||
crate::Error::CreateSnapshotError(format!(
|
||||
"failed to create process snapshot: {why}"
|
||||
)))
|
||||
}
|
||||
};
|
||||
))
|
||||
})?;
|
||||
let mut process_entry_32_w = PROCESSENTRY32W {
|
||||
dwSize: u32::try_from(size_of::<PROCESSENTRY32W>())?,
|
||||
..Default::default()
|
||||
};
|
||||
match unsafe { Process32FirstW(snapshot, &mut process_entry_32_w) } {
|
||||
Ok(()) => {}
|
||||
Err(why) => {
|
||||
return Err(crate::Error::CreateSnapshotError(format!(
|
||||
unsafe { Process32FirstW(snapshot, &mut process_entry_32_w) }.map_err(|why| {
|
||||
crate::Error::CreateSnapshotError(format!(
|
||||
"failed to get first process from snapshot: {why}"
|
||||
)))
|
||||
}
|
||||
};
|
||||
))
|
||||
})?;
|
||||
let mut processes = Vec::new();
|
||||
loop {
|
||||
let name = String::from_utf16_lossy(&process_entry_32_w.szExeFile)
|
||||
@@ -62,27 +57,21 @@ pub struct ModuleSnapshot {
|
||||
impl ModuleSnapshot {
|
||||
pub fn get_modules(pid: u32) -> Result<Vec<Self>, crate::Error> {
|
||||
let snapshot =
|
||||
match unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid) }
|
||||
{
|
||||
Ok(snapshot) => snapshot,
|
||||
Err(why) => {
|
||||
return Err(crate::Error::CreateSnapshotError(format!(
|
||||
unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, pid) }
|
||||
.map_err(|why| {
|
||||
crate::Error::CreateSnapshotError(format!(
|
||||
"failed to create module snapshot: {why}"
|
||||
)))
|
||||
}
|
||||
};
|
||||
))
|
||||
})?;
|
||||
let mut module_entry_32_w = MODULEENTRY32W {
|
||||
dwSize: u32::try_from(size_of::<MODULEENTRY32W>())?,
|
||||
..Default::default()
|
||||
};
|
||||
match unsafe { Module32FirstW(snapshot, &mut module_entry_32_w) } {
|
||||
Ok(()) => {}
|
||||
Err(why) => {
|
||||
return Err(crate::Error::CreateSnapshotError(format!(
|
||||
unsafe { Module32FirstW(snapshot, &mut module_entry_32_w) }.map_err(|why| {
|
||||
crate::Error::CreateSnapshotError(format!(
|
||||
"failed to get first module from snapshot: {why}"
|
||||
)))
|
||||
}
|
||||
}
|
||||
))
|
||||
})?;
|
||||
let mut modules = Vec::new();
|
||||
loop {
|
||||
let name = String::from_utf16_lossy(&module_entry_32_w.szModule)
|
||||
|
||||
Reference in New Issue
Block a user