diff --git a/dlopen2/src/raw/common.rs b/dlopen2/src/raw/common.rs index 17e0d77..062b8d0 100644 --- a/dlopen2/src/raw/common.rs +++ b/dlopen2/src/raw/common.rs @@ -5,14 +5,17 @@ use std::ffi::{CStr, CString, OsStr}; #[cfg(unix)] use super::unix::{ addr_info_cleanup, addr_info_init, addr_info_obtain, close_lib, get_sym, open_lib, open_self, - Handle, }; #[cfg(windows)] use super::windows::{ addr_info_cleanup, addr_info_init, addr_info_obtain, close_lib, get_sym, open_lib, open_self, - Handle, }; +#[cfg(unix)] +pub use super::unix::Handle; +#[cfg(windows)] +pub use super::windows::Handle; + use std::mem::{size_of, transmute_copy}; /** @@ -37,6 +40,7 @@ pub struct Library { } impl Library { + /** Open a dynamic library. @@ -116,7 +120,7 @@ impl Library { } /** - Obtain symbol from opened library. + Obtains a symbol from the opened library. **Note:** the `T` template type needs to have a size of a pointer. Because Rust does not support static casts at the moment, the size of the type @@ -152,6 +156,7 @@ impl Library { let cname = CString::new(name)?; self.symbol_cstr(cname.as_ref()) } + ///Equivalent of the `symbol` method but takes `CStr` as a argument. pub unsafe fn symbol_cstr(&self, name: &CStr) -> Result { //TODO: convert it to some kind of static assertion (not yet supported in Rust) @@ -169,6 +174,15 @@ impl Library { Ok(transmute_copy(&raw)) } } + + /** + Returns the raw OS handle for the opened library. + + This is `HMODULE` on Windows and `*mut c_void` on Unix systems. Don't use unless absolutely necessary. + */ + pub unsafe fn into_raw(&self) -> Handle { + return self.handle; + } } impl Drop for Library { diff --git a/dlopen2/src/raw/mod.rs b/dlopen2/src/raw/mod.rs index 94c5e6c..611c3d0 100644 --- a/dlopen2/src/raw/mod.rs +++ b/dlopen2/src/raw/mod.rs @@ -31,4 +31,4 @@ mod unix; #[cfg(windows)] mod windows; -pub use self::common::{AddressInfo, AddressInfoObtainer, Library, OverlappingSymbol}; +pub use self::common::{AddressInfo, AddressInfoObtainer, Library, OverlappingSymbol, Handle}; diff --git a/dlopen2/src/symbor/container.rs b/dlopen2/src/symbor/container.rs index c90c585..3b556fb 100644 --- a/dlopen2/src/symbor/container.rs +++ b/dlopen2/src/symbor/container.rs @@ -1,3 +1,5 @@ +use crate::raw; + use super::super::Error; use super::api::SymBorApi; use super::Library; @@ -73,6 +75,15 @@ where let api = T::load(static_ref)?; Ok(Self { api, lib }) } + + /** + Returns the raw OS handle for the opened library. + + This is `HMODULE` on Windows and `*mut c_void` on Unix systems. Don't use unless absolutely necessary. + */ + pub unsafe fn into_raw(&self) -> raw::Handle { + self.lib.into_raw() + } } impl Deref for Container diff --git a/dlopen2/src/symbor/library.rs b/dlopen2/src/symbor/library.rs index 612afe9..c096323 100644 --- a/dlopen2/src/symbor/library.rs +++ b/dlopen2/src/symbor/library.rs @@ -1,3 +1,5 @@ +use crate::raw; + use super::super::raw::Library as RawLib; use super::ptr_or_null::PtrOrNull; use super::ptr_or_null_mut::PtrOrNullMut; @@ -143,6 +145,15 @@ impl Library { pub unsafe fn reference_mut_cstr(&self, name: &CStr) -> Result<&mut T, Error> { self.lib.symbol_cstr(name) } + + /** + Returns the raw OS handle for the opened library. + + This is `HMODULE` on Windows and `*mut c_void` on Unix systems. Don't use unless absolutely necessary. + */ + pub unsafe fn into_raw(&self) -> raw::Handle { + self.lib.into_raw() + } } unsafe impl Send for Library {} diff --git a/dlopen2/src/wrapper/container.rs b/dlopen2/src/wrapper/container.rs index 916def3..47f5212 100644 --- a/dlopen2/src/wrapper/container.rs +++ b/dlopen2/src/wrapper/container.rs @@ -1,3 +1,5 @@ +use crate::raw; + use super::super::raw::Library; use super::super::Error; use super::api::WrapperApi; @@ -56,7 +58,7 @@ impl Container where T: WrapperApi, { - ///Open the library using provided file name or path and load all symbols. + /// Open the library using provided file name or path and load all symbols. pub unsafe fn load(name: S) -> Result, Error> where S: AsRef, @@ -65,7 +67,7 @@ where let api = T::load(&lib)?; Ok(Self { lib, api }) } - ///Load all symbols from the program itself. + /// Load all symbols from the program itself. /// /// This allows a shared library to load symbols of the program it was /// loaded into. @@ -75,7 +77,14 @@ where Ok(Self { lib, api }) } - ///Same as load(), except specify flags used by libc::dlopen + /// Returns the raw OS handle for the opened library. + /// + /// This is `HMODULE` on Windows and `*mut c_void` on Unix systems. Don't use unless absolutely necessary. + pub unsafe fn into_raw(&self) -> raw::Handle { + self.lib.into_raw() + } + + /// Same as load(), except specify flags used by libc::dlopen pub unsafe fn load_with_flags(name: S, flags: Option) -> Result, Error> where S: AsRef,