Skip to content

Commit

Permalink
https://github.com/nagisa/rust_libloading/issues/5
Browse files Browse the repository at this point in the history
  • Loading branch information
ubolonton committed Feb 25, 2018
1 parent ace52cf commit 55e7bf6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
7 changes: 2 additions & 5 deletions app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::ffi::CString;
use std::os::raw::c_char;

#[no_mangle]
pub fn get_message() -> *mut c_char {
CString::new(String::from("hello world")).unwrap().into_raw()
pub unsafe extern "C" fn testing() {
println!("YES!");
}
3 changes: 3 additions & 0 deletions main/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ authors = ["Damien Radtke <[email protected]>"]

[dependencies.app]
path = "../app"

[dependencies]
libloading = "0.4.3"
75 changes: 40 additions & 35 deletions main/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
use std::ffi::CString;
use std::os::raw;
use std::{thread, time};

const RTLD_NOW: raw::c_int = 2;
const LIB_PATH: &'static str = "../app/target/debug/libapp.dylib";

extern "C" {
fn dlopen(filename: *const raw::c_char, flags: raw::c_int) -> *mut raw::c_void;
fn dlclose(handle: *mut raw::c_void) -> raw::c_int;
fn dlsym(handle: *mut raw::c_void, symbol: *const raw::c_char) -> *mut raw::c_void;
fn dlerror() -> *mut raw::c_char;
}
// extern crate libloading;

// use std::os::raw;
// use std::{thread, time};
// use libloading::{Library, Symbol};

// const LIB_PATH: &'static str = "../app/target/debug/libapp.dylib";

// extern "C" {
// fn _dyld_image_count() -> u32;
// }

// fn main() {
// loop {
// unsafe {
// let i = _dyld_image_count();
// println!("{}", i);
// }

// thread::sleep(time::Duration::from_secs(1));

// let lib = Library::new(LIB_PATH)
// .expect("Library not found");

// unsafe {
// let init: Symbol<unsafe extern "C" fn()> =
// lib.get(b"init").expect("Symbol not found");
// init()
// };
// }
// }

extern crate libloading;

use libloading::{Library, Symbol};

fn main() {
loop {
unsafe {
thread::sleep(time::Duration::from_secs(1));
let handle = dlopen(CString::new(LIB_PATH).unwrap().into_raw(), RTLD_NOW);

let symbol = b"get_message";
let get_message = dlsym(handle, CString::new(&symbol[..]).unwrap().into_raw());
if get_message.is_null() {
println!("Failed to retrieve get_message symbol: {}", CString::from_raw(dlerror()).into_string().expect("Failed to retrieve dlerror"));
return;
}

let func = ::std::mem::transmute::<*mut raw::c_void, unsafe extern fn() -> *mut raw::c_char>(get_message);
let message = CString::from_raw(func()).into_string().unwrap();
println!("Message: {}", message);

if dlclose(handle) != 0 {
println!("Failed to close handle: {}", CString::from_raw(dlerror()).into_string().expect("Failed to retrieve dlerror"));
return;
}
}
}
let lib = Library::new("../app/target/debug/libapp.dylib").unwrap();
let sym: Symbol<unsafe extern "C" fn() -> ()> = unsafe { lib.get(b"testing") }.unwrap();
unsafe {
sym()
};
}

0 comments on commit 55e7bf6

Please sign in to comment.