PStack
2.0
Stack trace printer for MSVC and GCC binaries
|
Load shared libraries into the current process for execution. More...
#include <shared_library.hpp>
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_copyable & | operator= (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. | |
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.
using psystem::shared_library::library_version = uint32_t |
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
.
|
explicitprotectednoexcept |
Construct a shared library instance (without loading the module).
[in] | library_name | The name of the module to load. This must follow whatever the general rules for the system are (e.g., LoadLibrary for Windows). |
[in] | num_functions | The absolute number of functions to import from the shared library. This creates the size of the initial cache of function pointers. |
|
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.
|
protected |
Acquire a function pointer (by name) from a loaded library.
is_loaded()
== true
[in] | export_name | The name of the exported symbol. |
psystem::exception::system_exception | if 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.
psystem::exception::system_exception | if the library specified for this instance is not valid, or if there was a problem gathering the version information. |
|
noexcept |
Determines if the shared library is mapped into the process space.
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.
is_loaded()
== false
is_loaded()
== true
psystem::exception::system_exception | if the library could not be loaded. |
|
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.
is_loaded()
== true
N | The number of elements in the export_names parameter. This must exactly match the number of elements in m_functions . |
[in] | export_names | The names taken from the shared library's export table that will be used to gather addresses from the loaded module. |
|
noexcept |
Unload this library and clear the function pointers.
is_loaded()
== false