Skip to content

Instantly share code, notes, and snippets.

@Inari-Whitebear
Created January 13, 2015 12:17
Show Gist options
  • Save Inari-Whitebear/fcee70f34d2d3cdce396 to your computer and use it in GitHub Desktop.
Save Inari-Whitebear/fcee70f34d2d3cdce396 to your computer and use it in GitHub Desktop.
#[no_mangle]
pub extern "C" fn test() -> int {
return 1;
}
use std::dynamic_lib::DynamicLibrary;
use std::os;
fn print_type_of<T>(_: &T) -> () {
let type_name =
unsafe {
(*std::intrinsics::get_tydesc::<T>()).name
};
println!("{}", type_name);
}
fn load_plugin(raw_path: &'static str) {
let path = Path::new(raw_path);
let path = os::make_absolute(&path).unwrap();
let lib = match DynamicLibrary::open(Some(&path)) {
Ok(lib) => lib,
Err(error) => panic!("Could not load lib: {}", error)
};
let test: *mut fn() -> int = unsafe {
match lib.symbol::<fn() -> int>("test") {
Err(error) => panic!("Could not load function: {}", error),
Ok(test) => test
}
};
println!("a");
print_type_of(&test);
let c = unsafe {
(*test)()
};
print_type_of(&c);
println!("Returned: {}", c);
}
fn main() {
load_plugin("./plugins/plugintest.dll")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment