0

I'm newbie in Rust, and trying to compile Rust code into WASM:

use libloading::{Library, Symbol};
use std::ffi::{CStr, CString};
use std::os::raw::c_char;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn main() {
    // Load the DLL
    let lib = unsafe { Library::new("file.dll").unwrap() };

    let connect: Symbol<unsafe extern "C" fn(*const std::os::raw::c_char, *const std::os::raw::c_char) -> i32;> = 
                 unsafe { lib.get(b"Function\0").unwrap() };

But when i run wasm-pack i'm getting the error:

error[E0432]: unresolved imports 'libloading::Library', 'libloading::Symbol'
 --> src\lib.rs:1:18
  |
1 | use libloading::{Library, Symbol};
  |                  ^^^^^^^  ^^^^^^ no 'Symbol' in the root
  |                  |
  |                  no 'Library' in the root

For more information about this error, try 'rustc --explain E0432'`.
error: could not compile 'rust' due to previous error
Error: Compiling your crate to WebAssembly failed
Caused by: failed to execute 'cargo build': exited with exit code: 101
  full command: "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"`

If my undestanding is right - libloading can'not complie to WASM.

Does any one know way to comple such Rust code into WASM? Or may be there is any other approach to access functions from dll file in JS (React).

I'm trying:

  • change 'release' in toml file;
  • compile in binary file;
Jmb
  • 18,893
  • 2
  • 28
  • 55
scorp1k
  • 31
  • 3
  • also try to `cargo build --target wasm32-unknown-unknown`, but getting same error – scorp1k Jan 12 '23 at 08:06
  • 1
    AFAIK you can't access functions from a DLL inside WASM. If the WASM is intended to run inside a web browser, there is no solution (for security reasons, web browser won't allow access to a DLL from JS). If your code is intended to run on nodejs, you will need to wrap the DLL on the nodejs side, then you will be able to call it like you would call a JS function. – Jmb Jan 12 '23 at 08:13
  • Thank you for your answer. But I dont need to call dll file from user's machine. I'm planning to host C compiled dll file on a server with static js/wasm files and wasm will be like a wrapper for a functions in that dll file. Anyway the problem appears earlier on compiling step. – scorp1k Jan 12 '23 at 15:27
  • I'm trying another approach: ```#[link(name = "CustomDLL")] extern "C" { fn foo(arg1: *const c_char, arg2: *const c_char) -> i32; }``` But getting another error: ```rust-lld: error: unable to find library -lCustomDLL``` – scorp1k Jan 13 '23 at 09:27

0 Answers0