dllmain-rs 0.1.0

A proc-macro attribute to generate DllMain for Windows DLLs
Documentation

dllmain-rs

Crates.io Crates.io Downloads Docs.rs License

A proc macro to generate dllmain

Installation

Add this to your Cargo.toml:

[dependencies]
dllmain-rs = "0.1.0"

Options

#[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"

Valid entry function signatures

  • fn name()
  • fn name(reason: u32)

Rejected signatures include methods, async, const, unsafe, generic functions, variadics, non-() return types, and more than one argument.

Example usage

#[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
        }
        _ => {}
    }
}

Safety notes

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.

Standalone example crates

See examples/README.md for end-to-end cdylib consumer crates:

  • examples/minimal-dll
  • examples/lifecycle-dll

License

Licensed under either of

at your option.