Skip to main content

ModuleHandle

Struct ModuleHandle 

Source
pub struct ModuleHandle { /* private fields */ }

Implementations§

Source§

impl ModuleHandle

Source

pub fn open(lib_name: &str) -> Result<Self>

Examples found in repository?
examples/module_params.rs (line 34)
15fn main() -> dobby_rs_framework::Result<()> {
16    // EN: 1) `cast_fn` - turn a raw address into a typed function pointer.
17    // CN: 1) `cast_fn` - 把地址转换成带类型的函数指针。
18    let p = local_add as *const () as *mut c_void;
19    let f: fn(i32) -> i32 = unsafe { params::cast_fn(p) };
20    println!("cast_fn(local_add)(1) = {}", f(1));
21
22    // EN: 2) `read_ptr_value` / `write_ptr_value` - simple raw memory access.
23    // CN: 2) `read_ptr_value` / `write_ptr_value` - 简单的裸指针读写。
24    let mut buf = [0u8; 8];
25    let buf_ptr = buf.as_mut_ptr() as *mut c_void;
26    unsafe {
27        params::write_ptr_value(buf_ptr, 0x11223344u32);
28        let v: u32 = params::read_ptr_value(buf_ptr as *const c_void);
29        println!("read_ptr_value = 0x{v:08x}");
30    }
31
32    // EN: 3) `ModuleHandle` - open a module and resolve a symbol.
33    // CN: 3) `ModuleHandle` - 打开动态库并解析符号。
34    let m = ModuleHandle::open(common::DEMO_LIB)?;
35    println!("opened module: {}", m.lib_name());
36
37    // EN: Pick a tiny symbol per OS.
38    // CN: 选择一个各平台都常见的小符号。
39    #[cfg(windows)]
40    let sym = "GetCurrentThreadId";
41    #[cfg(unix)]
42    let sym = "puts";
43
44    let addr = m.wrapped_sym(sym);
45    println!("resolved {sym} = {:?}", addr);
46
47    // EN: `cast_ptr` is just a typed cast helper.
48    // CN: `cast_ptr` 只是一个带类型的转换辅助。
49    let _typed: *mut u8 = params::cast_ptr(buf_ptr);
50
51    // EN: CString is a common helper when calling `resolve` directly.
52    // CN: 如果直接用 `resolve`,通常要用 CString 来构造 CStr。
53    let _ = CString::new(sym).ok();
54    Ok(())
55}
Source

pub fn lib_name(&self) -> &str

Examples found in repository?
examples/module_params.rs (line 35)
15fn main() -> dobby_rs_framework::Result<()> {
16    // EN: 1) `cast_fn` - turn a raw address into a typed function pointer.
17    // CN: 1) `cast_fn` - 把地址转换成带类型的函数指针。
18    let p = local_add as *const () as *mut c_void;
19    let f: fn(i32) -> i32 = unsafe { params::cast_fn(p) };
20    println!("cast_fn(local_add)(1) = {}", f(1));
21
22    // EN: 2) `read_ptr_value` / `write_ptr_value` - simple raw memory access.
23    // CN: 2) `read_ptr_value` / `write_ptr_value` - 简单的裸指针读写。
24    let mut buf = [0u8; 8];
25    let buf_ptr = buf.as_mut_ptr() as *mut c_void;
26    unsafe {
27        params::write_ptr_value(buf_ptr, 0x11223344u32);
28        let v: u32 = params::read_ptr_value(buf_ptr as *const c_void);
29        println!("read_ptr_value = 0x{v:08x}");
30    }
31
32    // EN: 3) `ModuleHandle` - open a module and resolve a symbol.
33    // CN: 3) `ModuleHandle` - 打开动态库并解析符号。
34    let m = ModuleHandle::open(common::DEMO_LIB)?;
35    println!("opened module: {}", m.lib_name());
36
37    // EN: Pick a tiny symbol per OS.
38    // CN: 选择一个各平台都常见的小符号。
39    #[cfg(windows)]
40    let sym = "GetCurrentThreadId";
41    #[cfg(unix)]
42    let sym = "puts";
43
44    let addr = m.wrapped_sym(sym);
45    println!("resolved {sym} = {:?}", addr);
46
47    // EN: `cast_ptr` is just a typed cast helper.
48    // CN: `cast_ptr` 只是一个带类型的转换辅助。
49    let _typed: *mut u8 = params::cast_ptr(buf_ptr);
50
51    // EN: CString is a common helper when calling `resolve` directly.
52    // CN: 如果直接用 `resolve`,通常要用 CString 来构造 CStr。
53    let _ = CString::new(sym).ok();
54    Ok(())
55}
Source

pub fn lib_name_cstr(&self) -> &CStr

Source

pub fn raw(&self) -> *mut c_void

Source

pub fn resolve(&self, symbol: &CStr) -> Option<*mut c_void>

Examples found in repository?
examples/common/mod.rs (line 56)
54pub fn resolve_and_print(m: &ModuleHandle, symbol: &str) -> Result<()> {
55    let c = CString::new(symbol).map_err(|_| Error::InvalidInput)?;
56    let p = m.resolve(c.as_c_str()).ok_or(Error::SymbolNotFound)?;
57    println!("module.resolve({symbol}) = {p:p}");
58    Ok(())
59}
Source

pub fn wrapped_sym(&self, symbol: &str) -> Option<*mut c_void>

Examples found in repository?
examples/module_params.rs (line 44)
15fn main() -> dobby_rs_framework::Result<()> {
16    // EN: 1) `cast_fn` - turn a raw address into a typed function pointer.
17    // CN: 1) `cast_fn` - 把地址转换成带类型的函数指针。
18    let p = local_add as *const () as *mut c_void;
19    let f: fn(i32) -> i32 = unsafe { params::cast_fn(p) };
20    println!("cast_fn(local_add)(1) = {}", f(1));
21
22    // EN: 2) `read_ptr_value` / `write_ptr_value` - simple raw memory access.
23    // CN: 2) `read_ptr_value` / `write_ptr_value` - 简单的裸指针读写。
24    let mut buf = [0u8; 8];
25    let buf_ptr = buf.as_mut_ptr() as *mut c_void;
26    unsafe {
27        params::write_ptr_value(buf_ptr, 0x11223344u32);
28        let v: u32 = params::read_ptr_value(buf_ptr as *const c_void);
29        println!("read_ptr_value = 0x{v:08x}");
30    }
31
32    // EN: 3) `ModuleHandle` - open a module and resolve a symbol.
33    // CN: 3) `ModuleHandle` - 打开动态库并解析符号。
34    let m = ModuleHandle::open(common::DEMO_LIB)?;
35    println!("opened module: {}", m.lib_name());
36
37    // EN: Pick a tiny symbol per OS.
38    // CN: 选择一个各平台都常见的小符号。
39    #[cfg(windows)]
40    let sym = "GetCurrentThreadId";
41    #[cfg(unix)]
42    let sym = "puts";
43
44    let addr = m.wrapped_sym(sym);
45    println!("resolved {sym} = {:?}", addr);
46
47    // EN: `cast_ptr` is just a typed cast helper.
48    // CN: `cast_ptr` 只是一个带类型的转换辅助。
49    let _typed: *mut u8 = params::cast_ptr(buf_ptr);
50
51    // EN: CString is a common helper when calling `resolve` directly.
52    // CN: 如果直接用 `resolve`,通常要用 CString 来构造 CStr。
53    let _ = CString::new(sym).ok();
54    Ok(())
55}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.