pub unsafe fn read_ptr_value<T: Copy>(ptr: *const c_void) -> TExpand description
Read a value from a raw pointer (unaligned-safe).
§Safety
ptrmust be valid for reads ofsize_of::<T>()bytes.
Examples found in repository?
examples/module_params.rs (line 28)
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}