diff --git a/leechcore-sys/Cargo.toml b/leechcore-sys/Cargo.toml index a86cc0c..80449bf 100644 --- a/leechcore-sys/Cargo.toml +++ b/leechcore-sys/Cargo.toml @@ -8,6 +8,9 @@ license-file = "../LICENSE" links = "leechcore" +[dependencies] +ctor = "0.1" + [build-dependencies] cc = "1.0.62" bindgen = "0.55" diff --git a/leechcore-sys/build.rs b/leechcore-sys/build.rs index 368dc3a..8465fe2 100644 --- a/leechcore-sys/build.rs +++ b/leechcore-sys/build.rs @@ -43,6 +43,7 @@ fn build_leechcore(target: &str) { .collect::>(), ) .flag(&format!("-D{}", os_define())); + // EXPORTED_FUNCTION= to not export any symbols if !target.contains("windows") { // setup additional flags diff --git a/leechcore-sys/src/lib.rs b/leechcore-sys/src/lib.rs index ad009f8..02be8de 100644 --- a/leechcore-sys/src/lib.rs +++ b/leechcore-sys/src/lib.rs @@ -8,3 +8,28 @@ //#![allow(clippy::cognitive_complexity)] include!(concat!(env!("OUT_DIR"), "/leechcore.rs")); + +#[cfg(target_os = "windows")] +extern "C" { + pub fn DllMain(_: *const u8, _: u32, _: *const u8) -> u32; +} + +use ctor::{ctor, dtor}; + +#[ctor] +#[cfg(target_os = "windows")] +fn leechcore_attach() { + // DLL_PROCESS_ATTACH + unsafe { + DllMain(std::ptr::null_mut(), 1, std::ptr::null_mut()); + } +} + +#[dtor] +#[cfg(target_os = "windows")] +fn leechcore_detach() { + // DLL_PROCESS_DETACH + unsafe { + DllMain(std::ptr::null_mut(), 0, std::ptr::null_mut()); + } +}