A proc macro to generate dllmain
Add this to your Cargo.toml:
[dependencies]
dllmain-rs = "0.1.0"#[dllmain_rs::entry] accepts optional arguments:
events(process_attach, process_detach, thread_attach, thread_detach)panic = "abort" | "return_false"
Defaults:
events(process_attach)panic = "abort"
fn name()fn name(reason: u32)
Rejected signatures include methods, async, const, unsafe, generic functions,
variadics, non-() return types, and more than one argument.
#[dllmain_rs::entry]
fn on_process_attach() {
// lightweight, loader-lock-safe setup
}
#[dllmain_rs::entry(events(process_attach, process_detach), panic = "return_false")]
fn on_lifecycle(reason: u32) {
match reason {
1 => {
// process attach
}
0 => {
// process detach
}
_ => {}
}
}DllMain runs under the Windows loader lock. Keep logic minimal, avoid heavy I/O,
waiting on synchronization primitives, and complex re-entrancy during load/unload.
If your function panics, panic = "abort" terminates the process, while
panic = "return_false" returns FALSE from DllMain.
See examples/README.md for end-to-end cdylib consumer crates:
examples/minimal-dllexamples/lifecycle-dll
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.