memory_rs/internal/
macros.rs1#[macro_export]
5macro_rules! main_dll {
6 ($func:expr) => {
7 #[no_mangle]
8 #[allow(non_snake_case)]
9 pub extern "system" fn DllMain(
10 lib: windows_sys::Win32::Foundation::HINSTANCE,
11 reason: u32,
12 _: usize,
13 ) -> u32 {
14 unsafe {
15 match reason {
16 windows_sys::Win32::System::SystemServices::DLL_PROCESS_ATTACH => {
17 windows_sys::Win32::System::Threading::CreateThread(
18 std::ptr::null_mut(),
19 0,
20 Some($func),
21 lib as *const std::ffi::c_void,
22 0,
23 std::ptr::null_mut(),
24 );
25 }
26 _ => (),
27 };
28 }
29
30 return true as u32;
31 }
32 };
33}
34
35#[macro_export]
39macro_rules! count_args {
40 (@one $($t:tt)*) => { 1 };
41 ($(($($x:tt)*)),*$(,)?) => {
42 0 $(+ $crate::count_args!(@one $($x)*))*
43 };
44}
45
46#[macro_export]
48macro_rules! try_winapi {
49 ($call:expr) => {{
50 let res = $call;
51 if res == 0 {
52 let msg = format!(
53 "{} failed with error code {}",
54 std::stringify!($call),
55 std::io::Error::last_os_error()
56 );
57 return Err($crate::error::Error::new($crate::error::ErrorType::WinAPI, msg).into());
58 }
59 }};
60}
61
62#[macro_export]
75macro_rules! wrap_winapi {
76 ($call:expr, x $($tt:tt)*) => {
77 (|| -> Result<_, $crate::error::Error> {
78 let res = $call;
79 let x = res as usize;
80 if x $($tt)* {
81 let msg = format!(
82 "{} failed with error `{}`",
83 std::stringify!($call),
84 std::io::Error::last_os_error()
85 );
86 return Err($crate::error::Error::new(
87 $crate::error::ErrorType::WinAPI,
88 msg,
89 )
90 .into());
91 }
92 Ok(res)
93 })()
94 };
95}
96
97#[macro_export]
99macro_rules! scoped_no_mangle {
100 ($($name:ident: $v:ty = $val:expr;)*) => {
101 $(#[no_mangle] pub static mut $name: $v = $val;)*
102 }
103}
104
105#[macro_export]
107macro_rules! generate_aob_pattern {
108 [$($val:tt),* ] => {
109 $crate::internal::memory::MemoryPattern::new(
110 $crate::count_args!($(($val)),*),
111 |slice: &[u8]| -> bool {
112 matches!(slice, [$($val),*])
113 }
114 )
115 }
116}