use libffi::high::CodePtr;
use libffi::middle::{arg, Cif, Type};
use std::ffi::c_void;
#[cfg(target_os = "macos")]
const FILE: &str = "/usr/lib/libSystem.B.dylib";
#[cfg(target_os = "linux")]
const FILE: &str = "/lib/x86_64-linux-gnu/libc.so.6";
#[cfg(target_os = "windows")]
const FILE: &str = r"C:\Windows\System32\msvcrt.dll";
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
fn main() {
println!("This example is only supported on macOS, Linux, and Windows.");
}
#[cfg(any(target_os = "macos", target_os = "linux", target_os = "windows"))]
fn main() {
let func_name = "isdigit";
let cif = Cif::new(vec![Type::i32()], Type::i32());
let args1 = [arg(&('1' as i32))];
let args2 = [arg(&('a' as i32))];
unsafe {
let lib = libloading::Library::new(FILE).unwrap();
let converted_name = std::ffi::CString::new(func_name).unwrap();
let func: libloading::Symbol<fn() -> c_void> = lib.get(converted_name.as_bytes()).unwrap();
let func = func.into_raw().as_raw_ptr();
let result: i32 = cif.call(CodePtr(func as *mut _), &args1);
assert_eq!(result, 1);
let result: i32 = cif.call(CodePtr(func as *mut _), &args2);
assert_eq!(result, 0);
}
}