forked from dradtke/rust-dylib-issues
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/nagisa/rust_libloading/issues/5
- Loading branch information
Showing
3 changed files
with
45 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,6 @@ authors = ["Damien Radtke <[email protected]>"] | |
|
||
[dependencies.app] | ||
path = "../app" | ||
|
||
[dependencies] | ||
libloading = "0.4.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}; | ||
} |