Skip to content

Commit

Permalink
Merge pull request #10 from jgcodes2020/master
Browse files Browse the repository at this point in the history
Add function for getting raw handle from library
  • Loading branch information
OpenByteDev authored Feb 6, 2024
2 parents c50e261 + 0346b9b commit 27cc2c1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
20 changes: 17 additions & 3 deletions dlopen2/src/raw/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

/**
Expand All @@ -37,6 +40,7 @@ pub struct Library {
}

impl Library {

/**
Open a dynamic library.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<T>(&self, name: &CStr) -> Result<T, Error> {
//TODO: convert it to some kind of static assertion (not yet supported in Rust)
Expand All @@ -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;

Check failure on line 184 in dlopen2/src/raw/common.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

error: unneeded `return` statement --> dlopen2/src/raw/common.rs:184:9 | 184 | return self.handle; | ^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `-D clippy::needless-return` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(clippy::needless_return)]` help: remove `return` | 184 - return self.handle; 184 + self.handle |
}
}

impl Drop for Library {
Expand Down
2 changes: 1 addition & 1 deletion dlopen2/src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
11 changes: 11 additions & 0 deletions dlopen2/src/symbor/container.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::raw;

use super::super::Error;
use super::api::SymBorApi;
use super::Library;
Expand Down Expand Up @@ -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<T> Deref for Container<T>
Expand Down
11 changes: 11 additions & 0 deletions dlopen2/src/symbor/library.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -143,6 +145,15 @@ impl Library {
pub unsafe fn reference_mut_cstr<T>(&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 {}
Expand Down
15 changes: 12 additions & 3 deletions dlopen2/src/wrapper/container.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::raw;

use super::super::raw::Library;
use super::super::Error;
use super::api::WrapperApi;
Expand Down Expand Up @@ -56,7 +58,7 @@ impl<T> Container<T>
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<S>(name: S) -> Result<Container<T>, Error>
where
S: AsRef<OsStr>,
Expand All @@ -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.
Expand All @@ -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<S>(name: S, flags: Option<i32>) -> Result<Container<T>, Error>
where
S: AsRef<OsStr>,
Expand Down

0 comments on commit 27cc2c1

Please sign in to comment.