SoFunction
Updated on 2025-04-07

Rust uses libloader to call dynamic link library

introduction

Need to use Rust recentlyDynamic callDynamic link library, originally intended to be usedlibloadingYes, butlibloadingWhen calling a function in the dll, the parameters and return types must be determined at compile time. But then I found outlibloaderThis bag,libloaderIt is based onlibloadingYes, but it's morelibloaderconvenient.

We need one firstDynamic link library, we can usecargo create project-name --libCreate a dynamic link library project and modify itUse latercargo buildCompile, I wrote three types of functions

// 
#[no_mangle]
pub fn println(str: &str) { // There are parameters but no return value    println!("{}", str);
}
#[no_mangle]
pub fn add(a: usize, b: usize) -> usize { // There are parameters and return values    a + b
}
#[no_mangle]
pub fn print_hello() { // No parameters, no return value    println!("Hello");
}

Then usecargo create project-name --binCreate a project using dll

We copied the compiled dynamic link library to the root directory of the new project. The name of my link library is.dylibIt is a link library compiled from macOS. If you are using Linux or Windows, the suffix name will be.soand.dll

Then we need to install the libloader dependency, weIn-house[dependencies]Addlibloader: "0.1.4"

[dependencies]
libloader: "0.1.4"

The latest version is0.1.4, It is recommended to use the latest version, the latest version can be viewed here:libloader - : Rust Package Registry

Code

use libloader::libloading

Then we need to get the functions in the dynamic link library, where the function of each parameter is already identified in the code's comment. It is worth noting that if the function does not return a value, it can be used()replace.

get_libfn!("", "println", my_println, (), str: &str); // Get the dll function// ^Link library path ^Function in library ^Name of call ^Return value ^Arguments

Next, we can directly call it beforeget_libfnof"The name of the call"

my_println("Hello World"); // Output Hello World

The same is true for other functions, the complete code is:

// 
use libloader::libloading // First, you need to refer to the libloader's libloading, which indirectly confirms that the libloader is based on libloadingfn main() {
    get_libfn!("", "println", my_println, (), str: &str); // Get the dll function    // ^Link library path ^Function in library ^Name of call ^Return value ^Arguments    my_println("Hello World");
    get_libfn!("", "add", my_add, usize, a: usize, b: usize);
    println!("10 + 20 = {}", my_add(10, 20));
    get_libfn!("", "print_hello", my_print_hello, ());
    my_print_hello();
}

navigation:

libloader:libloader - : Rust Package Registry

libloader's Github:Qixinies/libloader: A easy-to-use dll loader for rust that based on libloading ()

The above is the detailed content of Rust using libloader to call the dynamic link library. For more information about the dynamic link library of Rust libloader, please follow my other related articles!