第一部分 完整示例
以下是一个完整的32位Rust DLL示例,包含所有必要的步骤和代码:
完整示例:创建32位DLL
1. 确保安装32位工具链
rustup target add i686-pc-windows-msvc
2. 创建项目
cargo new my_32bit_dll --lib
cd my_32bit_dll
3. 修改 Cargo.toml
[package]
name = "my_32bit_dll"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"] # 关键:生成动态链接库
[dependencies]
windows = { version = "0.54", features = [
"Win32_System_Diagnostics_Debug",
"Win32_Foundation"
]}
4. 修改 src/lib.rs
use std::ffi::{
c_void, CString};
use windows::Win32::System::Diagnostics::Debug::OutputDebugStringA;
// 简单的DLL入口点(可选)
#[no_mangle]
#[allow(non_snake_case)]
pub extern "stdcall" fn DllMain(
_hinst_dll: *const c_void,
reason: u32,
_reserved: *mut c_void,
) -> bool {
match reason {
1 => debug_print("DLL_PROCESS_ATTACH"),
0 => debug_print("DLL_PROCESS_DETACH"),
2 => debug_print("DLL_THREAD_ATTACH"),
3 => debug_print("DLL_THREAD_DETACH"),
_ => {
}
}
true
}
// 导出函数示例1:数学运算
#[no_mangle]
pub extern "C" fn calculate(a: i32, b: i32) -> i32 {
(a * b) + (a + b)
}
// 导出函数示例2:字符串转换
#[no_mangle]
pub extern "C" fn to_uppercase(input: *const i8) -> *mut i8 {
if input.is_null() {
return std::ptr::null_mut();
}
let c_str = unsafe {
std::ffi::CStr::from_ptr(input)

最低0.47元/天 解锁文章
582

被折叠的 条评论
为什么被折叠?



