From e9363700dfbb27d8cea9248da6b7bd23a2348f0d Mon Sep 17 00:00:00 2001 From: Janis Date: Fri, 14 Apr 2023 21:55:52 +0200 Subject: [PATCH] removed local-only 'utils' dependency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - switched to the ´widestring´ crate for u16string support - moved the error handling code from the utils crate to here. --- Cargo.toml | 8 ++++++-- src/lib.rs | 34 +++++++++++++++++++++++++--------- src/tests.rs | 2 +- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d7911cb..a32bed0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,12 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -winapi = {version = "0.3.9" , features = ["libloaderapi", "dbghelp", "ntdef", "processthreadsapi"] } -utils = { path = "../utils", features = ["win32-error"]} +winapi = {version = "0.3.9" , features = ["libloaderapi", "dbghelp", "ntdef", "processthreadsapi", "winerror", "errhandlingapi", "winuser"] } +widestring = "1.0.2" lazy_static = "1.0.0" thiserror = "1.0.0" log = "0.4.0" + +[dev-dependencies] +test-log = "0.2" +env_logger = "*" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 9694dc1..b5be32d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,6 @@ use std::{ use lazy_static::lazy_static; use log::error; -use utils::{string::ToWide, win32::Win32Error}; use winapi::{ shared::ntdef::{HANDLE, ULONG}, um::{ @@ -32,8 +31,8 @@ pub mod error { ImageHelpError(#[from] ImageHelpError), #[error(transparent)] NullError(#[from] NulError), - #[error(transparent)] - Win32Error(#[from] utils::win32::error::Win32Error), + #[error("win32 Error Code {0}")] + Win32Error(u32), } #[derive(Debug, thiserror::Error)] @@ -41,6 +40,22 @@ pub mod error { #[error("Module could not be found")] ModuleNotFound, } + + impl Error { + pub(crate) fn last_error() -> Self { + Self::Win32Error(unsafe { winapi::um::errhandlingapi::GetLastError() }) + } + } + + pub(crate) fn true_or_last_error(f: F) -> Result<(), Error> + where + F: FnOnce() -> bool, + { + match f() { + true => Ok(()), + false => Err(Error::last_error()), + } + } } pub struct ImageHelp { @@ -92,14 +107,14 @@ impl ImageHelp { ) -> Result<*const u8, error::Error> { let process_handle = self.process_handle.read().unwrap(); - let module_name_w = module_name.to_wide_null(); + let module_name_w = widestring::U16CString::from_str(module_name).expect("unexpected null"); let module_handle = unsafe { let handle = LoadLibraryW(module_name_w.as_ptr()); if handle == std::ptr::null_mut() { error!("Module handle {} is NULL.", module_name); - Err(error::ImageHelpError::ModuleNotFound) + Err(error::Error::last_error()) } else { Ok(handle) } @@ -121,7 +136,7 @@ impl ImageHelp { log::debug!("SymLoadModuleExW.."); - match utils::win32::error::true_or_last_error(|| unsafe { + match error::true_or_last_error(|| unsafe { dbghelp::SymLoadModuleExW( GetCurrentProcess(), null_mut(), @@ -134,7 +149,7 @@ impl ImageHelp { ) != 0 }) { Ok(_) => Ok(()), - Err(Win32Error { error_code: 0 }) => { + Err(error::Error::Win32Error(0)) => { log::debug!("Symbols already loaded."); Ok(()) } @@ -146,12 +161,13 @@ impl ImageHelp { unsafe { std::mem::MaybeUninit::::zeroed().assume_init() }; sym_info.SizeOfStruct = std::mem::size_of::() as ULONG; - let symbol_name_utf16 = function_name.to_wide_null(); + let symbol_name_utf16 = + widestring::U16CString::from_str(function_name).expect("unexpected null char"); sym_info.MaxNameLen = 255; log::debug!("SymFromNameW.."); - utils::win32::error::true_or_last_error(|| unsafe { + error::true_or_last_error(|| unsafe { dbghelp::SymFromNameW(*process_handle, symbol_name_utf16.as_ptr(), &mut sym_info) != 0 })?; diff --git a/src/tests.rs b/src/tests.rs index 25e44b9..d1fa13a 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,6 +1,6 @@ use super::*; -#[test] +#[test_log::test] fn find_message_box_procaddress() { let messagebox = ImageHelp::get() .find_function("User32.dll", "MessageBoxA")