macro_rules! get_libfn {
($lib_path: expr, $fn_name: expr, $call_name: ident, $ret: ty, $($v: ident: $t:ty),*) => { ... };
($lib_path: expr, $fn_name: expr, $call_name:ident, $ret: ty) => { ... };
}
Expand description
§Get a function from a dynamic link library
You need use libloader::libloading
first
lib_path
: the path of DLLfn_name
: The function name from dllcall_name
: The call function name offn_name
ret
: return type of the function if the function don’t have return value, use “()” instead(value: type)
: (variadic argument) The arguments of the function from dll
§Example
get_libfn!("libstd.dylib", "println", my_println, (), str: &str);
my_println("Hello World");
get_libfn!("libstd.dylib", "add", my_add, usize, a: usize, b: usize);
println!("10 + 20 = {}", my_add(10, 20));
get_libfn!("libstd.dylib", "print_hello", my_print_hello, ());
my_print_hello();
§the contents of libstd.dylib:
#[no_mangle]
pub fn println(str: &str) {
println!("{}", str);
}
#[no_mangle]
pub fn add(a: usize, b: usize) -> usize {
a + b
}
#[no_mangle]
pub fn print_hello() {
println!("Hello");
}