#[macro_export]
macro_rules! main_dll {
($func:expr) => {
#[no_mangle]
#[allow(non_snake_case)]
pub extern "system" fn DllMain(
lib: windows_sys::Win32::Foundation::HINSTANCE,
reason: u32,
_: usize,
) -> u32 {
unsafe {
match reason {
windows_sys::Win32::System::SystemServices::DLL_PROCESS_ATTACH => {
windows_sys::Win32::System::Threading::CreateThread(
std::ptr::null_mut(),
0,
Some($func),
lib as *const std::ffi::c_void,
0,
std::ptr::null_mut(),
);
}
_ => (),
};
}
return true as u32;
}
};
}
#[macro_export]
macro_rules! count_args {
(@one $($t:tt)*) => { 1 };
($(($($x:tt)*)),*$(,)?) => {
0 $(+ $crate::count_args!(@one $($x)*))*
};
}
#[macro_export]
macro_rules! try_winapi {
($call:expr) => {{
let res = $call;
if res == 0 {
let msg = format!(
"{} failed with error code {}",
std::stringify!($call),
std::io::Error::last_os_error()
);
return Err($crate::error::Error::new($crate::error::ErrorType::WinAPI, msg).into());
}
}};
}
#[macro_export]
macro_rules! wrap_winapi {
($call:expr, x $($tt:tt)*) => {
(|| -> Result<_, $crate::error::Error> {
let res = $call;
let x = res as usize;
if x $($tt)* {
let msg = format!(
"{} failed with error `{}`",
std::stringify!($call),
std::io::Error::last_os_error()
);
return Err($crate::error::Error::new(
$crate::error::ErrorType::WinAPI,
msg,
)
.into());
}
Ok(res)
})()
};
}
#[macro_export]
macro_rules! scoped_no_mangle {
($($name:ident: $v:ty = $val:expr;)*) => {
$(#[no_mangle] pub static mut $name: $v = $val;)*
}
}
#[macro_export]
macro_rules! generate_aob_pattern {
[$($val:tt),* ] => {
$crate::internal::memory::MemoryPattern::new(
$crate::count_args!($(($val)),*),
|slice: &[u8]| -> bool {
matches!(slice, [$($val),*])
}
)
}
}