I've been trying to get a basic proof of concept up and running using cmake and rust here https://github.com/shadowmint/rust-dl-example, but to no avail.
The basic idea is to build a DLL and then load symbols like this:
let dl = DynamicLibrary::open(Some(dl_path));
match dl {
Ok(dll) => {
// Try loading symbols. Note that because of the api we can't
// directly pass T as fn(c_int) -> c_int, because the return is
// a pointer, not a pointer to a pointer.
unsafe{
rtn.foo = match dll.symbol::<c_void>("foo") {
Ok(symbol) => Some(transmute(symbol)),
Err(_) => None
};
rtn.bar = match dll.symbol::<c_void>("bar") {
Ok(symbol) => Some(transmute(symbol)),
Err(_) => None
};
trace!("Read: {}", dll.symbol::<c_void>("foo"));
trace!("Read: {}", dll.symbol::<c_void>("bar"));
rtn.lib = Some(dll);
}
}
Which works a charm on linux and osx, but sadly fails on windows:
Compiling dltest v0.1.0 (file:///C:/projects/rust-all/rust-dl-example)
Running target\dltest-3ed01b3dac66913e.exe
running 1 test
Pattern: Some(C:\projects\rust-all\rust-dl-example\build\*foo*.dll)
Some(C:\projects\rust-all\rust-dl-example\build\libfoo.dll)
Read: Err(Error code 127)
Read: Err(Error code 127)
Successfully loaded table
Done1
Done2
stack backtrace:
1: 0x5452c8 - main
2: 0x549525 - main
3: 0x54effd - main
4: 0x54ecc2 - main
5: 0x4d8e8d - main
6: 0x402411
7: 0x4023e3
8: 0x40238d
9: 0x40ccbc
10: 0x43d438 - main
11: 0x52e277 - main
12: 0x54d4cc - main
13: 0x54fc0f - main
14: 0x54fbe9 - main
15: 0x54d55e - main
16: 0x54d309 - main
17: 0x54d116 - main
18: 0x54e64d - main
19: 0x76fc652d - BaseThreadInitThunk
task failed during unwinding. aborting.
Error code 127 is windows magic for 'no symbol found', but first of all it isn't returning as an error correctly and secondly, I can't see anything wrong with my DLL. It works fine from a c program... and it doesn't have any weird linkages:
What's going on? Anyone got a working example of a DLL with windows & rust?
My guess is that you are not correctly matching the calling convention of your DLL. I used this code to create a DLL:
#[no_mangle]
// Note explicit calling convention
pub extern "C" fn foo_add_2(a: u32) -> u32 {
a + 2
}
Compiled that as:
rustc --crate-type=dylib
And accessed it as:
use std::dynamic_lib::DynamicLibrary;
use std::mem::transmute;
fn main() {
let lib = match DynamicLibrary::open(Some("lib")) {
Ok(x) => x,
Err(x) => panic!("{}", x),
};
// Note explicit calling convention
let meth: extern "C" fn(u32) -> u32 = unsafe {
match lib.symbol::<u8>("foo_add_2") {
Ok(x) => transmute(x),
Err(x) => panic!("{}", x),
}
};
let input = 5;
let res = meth(input);
println!("Hello, library! ({} + 2 => {})", input, res);
}
This worked fine on a 32-bit VM with Windows 7 and a Rust nightly from 2014-12-08.
However, if I change the calling conventions to not match, or leave it off, I see your error 127
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With