ç”Ÿæˆ DLL 转å‘的函数。
先在 Cargo.toml
ä¸æ·»åŠ ä¾èµ–:
toml
[build-dependencies]
forward-dll = "0.1.11"
在 build.rs
ä¸æ·»åŠ 如下代ç :
```rust use forwarddll::forwarddll;
fn main() { forward_dll("C:\Windows\System32\version.dll").unwrap(); } ```
rust
use forward_dll::forward_dll_with_exports;
forward_dll_with_exports(
"C:\\Windows\\system32\\version.dll",
&[
(1, "GetFileVersionInfoA"),
(2, "GetFileVersionInfoByHandle"),
(3, "GetFileVersionInfoExA"),
(4, "GetFileVersionInfoExW"),
(5, "GetFileVersionInfoSizeA"),
(6, "GetFileVersionInfoSizeExA"),
(7, "GetFileVersionInfoSizeExW"),
(8, "GetFileVersionInfoSizeW"),
(9, "GetFileVersionInfoW"),
(10, "VerFindFileA"),
(11, "VerFindFileW"),
(12, "VerInstallFileA"),
(13, "VerInstallFileW"),
(14, "VerLanguageNameA"),
(15, "VerLanguageNameW"),
(16, "VerQueryValueA"),
(17, "VerQueryValueW"),
],
)
.unwrap();
```rust use forward_dll::ForwardModule;
pub struct VersionModule;
const VERSION_LIB: VersionModule = VersionModule;
pub extern "system" fn DllMain(inst: isize, reason: u32, _: *const u8) -> u32 { if reason == 1 { println!("==> version.dll loaded"); VERSIONLIB.init().unwrap(); println!("==> version.dll initialized"); } 1 } ```
注æ„,#[forward(target = "path/of/your/dll")]
ä¸çš„路径,应在编译期å¯ä»¥è®¿é—®åˆ°ï¼ˆè¿‡ç¨‹å®ä¼šè¯»å–这个文件并æå–出导出表),如果这个路径为相对路径,则应相对于 Cargo.toml
所在的目录。
```rust forwarddll::forwarddll!( "C:\Windows\system32\version.dll", DLLVERSIONFORWARDER, GetFileVersionInfoA GetFileVersionInfoByHandle GetFileVersionInfoExA GetFileVersionInfoExW GetFileVersionInfoSizeA GetFileVersionInfoSizeExA GetFileVersionInfoSizeExW GetFileVersionInfoSizeW GetFileVersionInfoW VerFindFileA VerFindFileW VerInstallFileA VerInstallFileW VerLanguageNameA VerLanguageNameW VerQueryValueA VerQueryValueW );
pub extern "system" fn DllMain(inst: isize, reason: u32, _: *const u8) -> u32 { if reason == 1 { // 这里è¦è‡ªè¡ŒæŒæœ‰åº•å±‚çš„ version.dll çš„å¥æŸ„,防æ¢è¢«é‡Šæ”¾ã€‚ let _ = forwarddll::utils::loadlibrary("C:\Windows\system32\version.dll"); // 调用 forwardall æ–¹æ³•ï¼Œå»ºç«‹å¯¼å‡ºå‡½æ•°ä¸Žç›®æ ‡å‡½æ•°ä¹‹é—´çš„æ˜ å°„å…³ç³»ã€‚ let _ = unsafe { DLLVERSIONFORWARDER.forward_all() }; } 1 } ```
powershell
cargo build -p version; cargo run -p just-call-version
Copyright (c) 2022-present, hamflx