How do you use DynamicLibrary with dlls on windows in rust?

I tried to get a basic proof of concept before and after using cmake and rust here https://github.com/shadowmint/rust-dl-example , but to no avail.

The basic idea is to create a DLL and then load the following symbols:

  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 in linux and osx style, but sadly fails in 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 the magic of windows for "no symbol found", but first of all it does not return correctly as an error, and secondly, I see nothing wrong with my DLL. It works fine with the c ... program and it has no strange connections:

Its fine

What's happening? Has anyone got a working example of a DLL with windows and rust?

+4
2

, DLL. DLL:

#[no_mangle]
// Note explicit calling convention
pub extern "C" fn foo_add_2(a: u32) -> u32 {
  a + 2
}

:

rustc --crate-type=dylib

:

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);
}

32- Windows 7 Rust 2014-12-08.

, , , , error 127.

+2

All Articles