Template project for creating Windows shared libraries (.dll)
You can either install cargo-generate
tool with cargo install cargo-generate
or clone this repository.
# Using `cargo-generate` tool
cargo generate https://github.com/sy1ntexx/rusty-dll
# Or clone the project itself
git clone https://github.com/sy1ntexx/rusty-dll
Don't forget to rename package to your project name in Cargo.toml
build/
folder contains batch scripts to quickly build your packet for x64
or x86
architecture.
By default, cargo
will compile for x64
bit architecture because of .cargo/config
.
If you want to target x86
you need to either change the target in configuration file,
or take a use of handy scripts in the build
folder.
Write the code you want in main_thread
function.
#[no_mangle]
unsafe extern "C" fn DllMain(
hinst_dll: usize,
fdw_reason: u32,
_lp_reserved: usize) -> i32
{
match fdw_reason {
1 => {
std::thread::spawn(move || {
unsafe { main_thread(hinst_dll) }
});
}
_ => { return FALSE; }
}
1
}
unsafe extern "system" fn main_thread(lp_thread_parameter: usize) -> u32 {
// AllocConsole();
println!("Hello from #RustLang!");
// Put your code here
// FreeConsole();
FreeLibraryAndExitThread(lp_thread_parameter as _, 0);
0
}
After successfull build, you can find your dll in target/TARGET_NAME/[debug/release]/