PStack  2.0
Stack trace printer for MSVC and GCC binaries
psystem::shared_library Class Reference

Load shared libraries into the current process for execution. More...

#include <shared_library.hpp>

Inheritance diagram for psystem::shared_library:
Collaboration diagram for psystem::shared_library:

Classes

struct  loaded_function
 Encapsulate a loaded function's address into a callable functor. More...
 
struct  loaded_function< T, std::true_type >
 Provide a specialization for loaded_function to handle a void return type. More...
 
struct  loaded_function_base
 Common base class for all specializations of loaded_function. More...
 

Type Declarations

using library_version = uint32_t
 Type for shared library versions. More...
 

Construction / Destruction

 shared_library (std::string library_name, size_t num_functions) noexcept
 Construct a shared library instance (without loading the module). More...
 
 ~shared_library () noexcept
 Clean up the shared library. More...
 

Public Interface

library_version get_version () const
 Access the version for the library that this instance wraps. More...
 
bool is_loaded () const noexcept
 Determines if the shared library is mapped into the process space. More...
 
void load ()
 Loads the requested library into the current process space. More...
 
void unload () noexcept
 Unload this library and clear the function pointers. More...
 

Inherited Interface

FARPROC get_function (char const *export_name) const
 Acquire a function pointer (by name) from a loaded library. More...
 
template<size_t N>
void load_all_functions (char const *export_names[N])
 Fully populate the m_functions data with addresses. More...
 

Member Data

std::vector< FARPROC > m_functions
 The cache of function pointers gathered from a loaded library.
 
psystem::shared_handle< HMODULE, nullptr > m_library_handle
 The system handle to the loaded library. It may be nullptr.
 
std::string m_library_name
 The (passed-in) name of the library that this class represents.
 

Additional Inherited Members

- Protected Member Functions inherited from psystem::not_copyable
 not_copyable (not_copyable const &)=delete
 Delete the copy constructor.
 
not_copyableoperator= (not_copyable const &)=delete
 Delete the assignment operator.
 
constexpr not_copyable () noexcept
 A simple CTOR suitable for noexcept and constexpr uses.
 
 ~not_copyable () noexcept
 A simple CTOR suitable for noexcept uses.
 

Detailed Description

Load shared libraries into the current process for execution.

This class loads shared libraries (SO) and dynamic link libraries (DLL) into the current process so that automatic binding does not occur. This is interesting because we may wish to check the version of a library before using it (a strange error occurs when symbols are not found), or perhaps we may fall back to another bit of functionality if the libraries to support the preferred aproach are not present on the system.

This class should be a base class, and specific DLLs and SOs should be encapsulated by their own derived class.

Note
Copy and move construction of this class is certainly feasible, but not needed, so for now this functionality does not exist here.
Author
Matt Bisson
Date
19 November, 2015
Since
PSystem 2.0
Version
PSystem 2.0

Member Typedef Documentation

Type for shared library versions.

When viewed as an integer value, the 16 most significant bits represent a major version, while the lowest 16 represent a minor version. For example, a version of 4.15 would manifest as 0x00040015.

Constructor & Destructor Documentation

psystem::shared_library::shared_library ( std::string  library_name,
size_t  num_functions 
)
explicitprotectednoexcept

Construct a shared library instance (without loading the module).

Parameters
[in]library_nameThe name of the module to load. This must follow whatever the general rules for the system are (e.g., LoadLibrary for Windows).
[in]num_functionsThe absolute number of functions to import from the shared library. This creates the size of the initial cache of function pointers.
psystem::shared_library::~shared_library ( )
noexcept

Clean up the shared library.

This will decrease the reference count on the shared library, possibly causing the system to unload it from the process space. All function pointers acquired from this instance may become invalidated.

Member Function Documentation

FARPROC psystem::shared_library::get_function ( char const *  export_name) const
protected

Acquire a function pointer (by name) from a loaded library.

Precondition
is_loaded() == true
Parameters
[in]export_nameThe name of the exported symbol.
Returns
The pointer to the function acquired from the library.
Exceptions
psystem::exception::system_exceptionif the specified symbol could not be found in the loaded shared library.
shared_library::library_version psystem::shared_library::get_version ( ) const

Access the version for the library that this instance wraps.

Returns
The associated library version information relevant to this instance.
Exceptions
psystem::exception::system_exceptionif the library specified for this instance is not valid, or if there was a problem gathering the version information.
Todo:
Better exception
bool psystem::shared_library::is_loaded ( ) const
noexcept

Determines if the shared library is mapped into the process space.

Returns
true if the shared library is mapped into the current process space, and the handle is contained within this instance. This returns false, otherwise.
void psystem::shared_library::load ( )

Loads the requested library into the current process space.

No function pointers will be acquired as a result of this call.

Precondition
is_loaded() == false
Postcondition
is_loaded() == true
Exceptions
psystem::exception::system_exceptionif the library could not be loaded.
Bug:
shared_handle::decrement causes us to lose the deleter (as the control block is deleted). The managed_handle_proxy (triggered from operator&), however only calls the version of shared_handle::reset() that does not take a custom deleter.
template<size_t N>
void psystem::shared_library::load_all_functions ( char const *  export_names[N])
inlineprotected

Fully populate the m_functions data with addresses.

This function completely replaces the contents of the m_functions member data. You must pass a simply array of C-style strings, which will then be used to load the function addresses.

Precondition
is_loaded() == true
Template Parameters
NThe number of elements in the export_names parameter. This must exactly match the number of elements in m_functions.
Parameters
[in]export_namesThe names taken from the shared library's export table that will be used to gather addresses from the loaded module.
void psystem::shared_library::unload ( )
noexcept

Unload this library and clear the function pointers.

Postcondition
is_loaded() == false

The documentation for this class was generated from the following files: