use winapi::shared::minwindef::{ HINSTANCE, DWORD, LPVOID, BOOL, TRUE }; use winapi::um::winnt::{ DLL_PROCESS_DETACH, DLL_PROCESS_ATTACH, DLL_THREAD_ATTACH, DLL_THREAD_DETACH }; use winapi::um::libloaderapi::{ DisableThreadLibraryCalls }; use winapi::um::winuser::{ MessageBoxA, MessageBoxW, MB_OK }; use std::ptr::null_mut; use std::ffi::OsStr; use std::os::windows::ffi::OsStrExt as _; fn wide_string(s: &str) -> Vec { OsStr::new(s).encode_wide().chain(Some(0)).collect() } fn show_message_box(caption: &str, text: &str) { unsafe{ MessageBoxW( null_mut() as _, wide_string(text).as_ptr() as _, wide_string(caption).as_ptr() as _, MB_OK ); } } // define dllmain to handle the init action #[no_mangle] #[allow(non_snake_case)] unsafe extern "system" fn DllMain(hinst: HINSTANCE, reason: DWORD, _reserved: LPVOID) -> BOOL { match reason { DLL_PROCESS_DETACH => { println!("Remove from main process."); } DLL_PROCESS_ATTACH => unsafe { println!("Attach to main process."); show_message_box("Hello World", "I'm injected by Rust."); DisableThreadLibraryCalls(hinst); }, DLL_THREAD_ATTACH => {} DLL_THREAD_DETACH => {} _ => {} }; return TRUE; }