From fed6991b2f6b154792c5c515dcd4bad4dc6d8885 Mon Sep 17 00:00:00 2001 From: jgcodes2020 Date: Tue, 30 Jan 2024 21:09:40 -0500 Subject: [PATCH 1/2] add function for getting raw handle --- dlopen2/src/raw/common.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/dlopen2/src/raw/common.rs b/dlopen2/src/raw/common.rs index add5e28..7c946bf 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. @@ -82,7 +86,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 @@ -118,6 +122,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) @@ -135,6 +140,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 { From f1927c9f44db920e55c9fdba14d068b6948f73a5 Mon Sep 17 00:00:00 2001 From: jgcodes2020 Date: Tue, 30 Jan 2024 22:29:27 -0500 Subject: [PATCH 2/2] add into_raw for containers --- dlopen2/src/raw/mod.rs | 2 +- dlopen2/src/symbor/container.rs | 11 +++++++++++ dlopen2/src/symbor/library.rs | 11 +++++++++++ dlopen2/src/wrapper/container.rs | 11 +++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) 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 57a97e7..b9889c8 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; @@ -74,6 +76,15 @@ where let api = T::load(&lib)?; Ok(Self { lib, api }) } + + /** + 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