forked from ahmed-masud/rust-dlopen
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b408a1c
commit 33a82db
Showing
42 changed files
with
342 additions
and
334 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,69 +1,7 @@ | ||
[package] | ||
name = "dlopen2" | ||
version = "0.5.0" | ||
authors = [ | ||
"Szymon Wieloch <szymon.wieloch@gmail.com>", | ||
"Ahmed Masud <ahmed.masud@saf.ai>", | ||
"OpenByte <development.openbyte@gmail.com>"] | ||
description = "Library for opening and operating on dynamic link libraries (also known as shared objects or shared libraries)." | ||
keywords = [ | ||
#common functions | ||
"dlopen", "dll", "so", "dylib", "shared"] | ||
license = "MIT" | ||
repository = "https://github.com/OpenByteDev/dlopen2" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
dlopen2_derive = { path = "dlopen2-derive", version = "0.3", optional = true } | ||
once_cell = "1.18" | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
winapi = { version = "0.3", features = ["winnt", "minwindef", "winerror", "libloaderapi", "errhandlingapi", "dbghelp", "processthreadsapi", "basetsd"] } | ||
|
||
[target.'cfg(unix)'.dependencies] | ||
libc = "0.2" | ||
|
||
[dev-dependencies] | ||
const-cstr = "0.3" | ||
regex = "1.8" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
example_dylib = { path = "example-dylib" } | ||
current_platform = "0.2" | ||
|
||
|
||
[features] | ||
default = ["wrapper", "symbor", "derive"] | ||
wrapper = [] | ||
symbor = [] | ||
derive = ["dlopen2_derive"] | ||
doc_cfg = [] | ||
|
||
|
||
[[example]] | ||
name = "raw" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "symbor" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "symbor_api" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "wrapper_api" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "raw_addr_info" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "wrapper_multi_api" | ||
crate-type = ["bin"] | ||
|
||
|
||
[package.metadata.docs.rs] | ||
all-features = true | ||
[workspace] | ||
members = [ | ||
"dlopen2", | ||
"dlopen2-derive", | ||
"example-dylib", | ||
] | ||
resolver = "2" |
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
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
[package] | ||
name = "dlopen2" | ||
version = "0.5.0" | ||
authors = [ | ||
"Szymon Wieloch <szymon.wieloch@gmail.com>", | ||
"Ahmed Masud <ahmed.masud@saf.ai>", | ||
"OpenByte <development.openbyte@gmail.com>"] | ||
description = "Library for opening and operating on dynamic link libraries (also known as shared objects or shared libraries)." | ||
keywords = [ | ||
"dlopen", "dll", "so", "dylib", "shared"] | ||
license = "MIT" | ||
repository = "https://github.com/OpenByteDev/dlopen2" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
dlopen2_derive = { path = "../dlopen2-derive", version = "0.3", optional = true } | ||
once_cell = "1.18" | ||
|
||
[target.'cfg(windows)'.dependencies] | ||
winapi = { version = "0.3", features = ["winnt", "minwindef", "winerror", "libloaderapi", "errhandlingapi", "dbghelp", "processthreadsapi", "basetsd"] } | ||
|
||
[target.'cfg(unix)'.dependencies] | ||
libc = "0.2" | ||
|
||
[dev-dependencies] | ||
const-cstr = "0.3" | ||
regex = "1.8" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" | ||
example_dylib = { path = "../example-dylib" } | ||
current_platform = "0.2" | ||
|
||
|
||
[features] | ||
default = ["wrapper", "symbor", "derive"] | ||
wrapper = [] | ||
symbor = [] | ||
derive = ["dlopen2_derive"] | ||
doc_cfg = [] | ||
|
||
|
||
[[example]] | ||
name = "raw" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "symbor" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "symbor_api" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "wrapper_api" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "raw_addr_info" | ||
crate-type = ["bin"] | ||
|
||
[[example]] | ||
name = "wrapper_multi_api" | ||
crate-type = ["bin"] | ||
|
||
|
||
[package.metadata.docs.rs] | ||
all-features = true |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
136 changes: 68 additions & 68 deletions
136
examples/wrapper_multi_api.rs → dlopen2/examples/wrapper_multi_api.rs
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,68 +1,68 @@ | ||
mod commons; | ||
use commons::example_lib_path; | ||
|
||
use dlopen2::wrapper::{Container, WrapperApi, WrapperMultiApi}; | ||
use std::os::raw::c_int; | ||
|
||
//Define 3 APIs: | ||
|
||
#[derive(WrapperApi)] | ||
struct Working1<'a> { | ||
rust_fun_print_something: fn(), | ||
c_fun_add_two: unsafe extern "C" fn(arg: c_int) -> c_int, | ||
rust_i32_mut: &'a mut i32, | ||
} | ||
|
||
#[derive(WrapperApi)] | ||
struct Working2<'a> { | ||
rust_fun_add_one: fn(arg: i32) -> i32, | ||
c_fun_print_something_else: extern "C" fn(), | ||
rust_i32: &'a i32, | ||
} | ||
|
||
//this one wont' work in the example | ||
#[derive(WrapperApi)] | ||
struct NotWorking<'a> { | ||
some_rust_fun: fn(arg: i32) -> i32, | ||
some_c_fun: extern "C" fn(), | ||
some_rust_num: &'a u32, | ||
} | ||
|
||
//Now define a multi wrapper that wraps sub APIs into one bigger API. | ||
//This example assumes that the first API is obligatory and the other two are optional. | ||
|
||
#[derive(WrapperMultiApi)] | ||
struct Api<'a> { | ||
pub obligatory: Working1<'a>, | ||
pub optional1: Option<Working2<'a>>, | ||
pub optional2: Option<NotWorking<'a>>, | ||
} | ||
|
||
fn main() { | ||
let lib_path = example_lib_path(); | ||
let api: Container<Api> = unsafe { Container::load(lib_path) }.expect("Could not open library"); | ||
//use obligatory API: | ||
api.obligatory.rust_fun_print_something(); | ||
println!("4+2={}", unsafe { api.obligatory.c_fun_add_two(4) }); | ||
println!("static i32={}", api.obligatory.rust_i32_mut()); | ||
|
||
match api.optional1 { | ||
Some(ref opt) => { | ||
println!("First optional API loaded!"); | ||
println!("3+1={}", opt.rust_fun_add_one(3)); | ||
opt.c_fun_print_something_else(); | ||
println!("static value is {}", opt.rust_i32()) | ||
} | ||
None => println!("Could not load the first optional API"), | ||
} | ||
|
||
match api.optional2 { | ||
Some(ref opt) => { | ||
opt.some_c_fun(); | ||
println!("Second optional API loaded"); | ||
println!("result of some function: {}", opt.some_rust_fun(3)); | ||
println!("static value is {}", opt.some_rust_num()); | ||
} | ||
None => println!("Could not load the second optional API"), | ||
} | ||
} | ||
mod commons; | ||
use commons::example_lib_path; | ||
|
||
use dlopen2::wrapper::{Container, WrapperApi, WrapperMultiApi}; | ||
use std::os::raw::c_int; | ||
|
||
//Define 3 APIs: | ||
|
||
#[derive(WrapperApi)] | ||
struct Working1<'a> { | ||
rust_fun_print_something: fn(), | ||
c_fun_add_two: unsafe extern "C" fn(arg: c_int) -> c_int, | ||
rust_i32_mut: &'a mut i32, | ||
} | ||
|
||
#[derive(WrapperApi)] | ||
struct Working2<'a> { | ||
rust_fun_add_one: fn(arg: i32) -> i32, | ||
c_fun_print_something_else: extern "C" fn(), | ||
rust_i32: &'a i32, | ||
} | ||
|
||
//this one wont' work in the example | ||
#[derive(WrapperApi)] | ||
struct NotWorking<'a> { | ||
some_rust_fun: fn(arg: i32) -> i32, | ||
some_c_fun: extern "C" fn(), | ||
some_rust_num: &'a u32, | ||
} | ||
|
||
//Now define a multi wrapper that wraps sub APIs into one bigger API. | ||
//This example assumes that the first API is obligatory and the other two are optional. | ||
|
||
#[derive(WrapperMultiApi)] | ||
struct Api<'a> { | ||
pub obligatory: Working1<'a>, | ||
pub optional1: Option<Working2<'a>>, | ||
pub optional2: Option<NotWorking<'a>>, | ||
} | ||
|
||
fn main() { | ||
let lib_path = example_lib_path(); | ||
let api: Container<Api> = unsafe { Container::load(lib_path) }.expect("Could not open library"); | ||
//use obligatory API: | ||
api.obligatory.rust_fun_print_something(); | ||
println!("4+2={}", unsafe { api.obligatory.c_fun_add_two(4) }); | ||
println!("static i32={}", api.obligatory.rust_i32_mut()); | ||
|
||
match api.optional1 { | ||
Some(ref opt) => { | ||
println!("First optional API loaded!"); | ||
println!("3+1={}", opt.rust_fun_add_one(3)); | ||
opt.c_fun_print_something_else(); | ||
println!("static value is {}", opt.rust_i32()) | ||
} | ||
None => println!("Could not load the first optional API"), | ||
} | ||
|
||
match api.optional2 { | ||
Some(ref opt) => { | ||
opt.some_c_fun(); | ||
println!("Second optional API loaded"); | ||
println!("result of some function: {}", opt.some_rust_fun(3)); | ||
println!("static value is {}", opt.some_rust_num()); | ||
} | ||
None => println!("Could not load the second optional API"), | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.