| Version: 3.3.1

Public Member Functions | Static Public Member Functions | List of all members
wxDynamicLibrary Class Reference
Class List by Category » Application and Process Management

`#include <wx/dynlib.h>`

## Detailed Description

wxDynamicLibrary is a class representing dynamically loadable library (Windows DLL, shared library under Unix etc).

Just create an object of this class to load a library and don't worry about unloading it – it will be done in the objects destructor automatically.

The following flags can be used with wxDynamicLibrary() or Load():

## Styles

This class supports the following styles:

- wxDL_LAZY:
  Equivalent of RTLD_LAZY under Unix, ignored elsewhere.
- wxDL_NOW:
  Equivalent of RTLD_NOW under Unix, ignored elsewhere.
- wxDL_GLOBAL:
  Equivalent of RTLD_GLOBAL under Unix, ignored elsewhere.
- wxDL_VERBATIM:
  Don't try to append the appropriate extension to the library name (this is done by default).
- wxDL_DEFAULT:
  Default flags, same as wxDL_NOW currently.
- wxDL_QUIET:
  Don't log an error message if the library couldn't be loaded.

##

Library: wxBase
Category: Application and Process Management

## Public Member Functions
| wxDynamicLibrary ()
| Default constructor. More...
| wxDynamicLibrary (const wxString &name, int flags=wxDL_DEFAULT)
| Constructor. More...
void | Attach (wxDllType h)
| Attaches the object to an existing handle. More...
wxDllType | Detach ()
| Detaches this object from its library handle. More...
void * | GetSymbol (const wxString &name, bool *success=0) const
| Returns pointer to symbol name in the library or nullptr if the library contains no such symbol. More...
void * | GetSymbolAorW (const wxString &name) const
| This function is available only under Windows as it is only useful when dynamically loading symbols from standard Windows DLLs. More...
bool | HasSymbol (const wxString &name) const
| Returns true if the symbol with the given name is present in the dynamic library, false otherwise. More...
bool | IsLoaded () const
| Returns true if the library was successfully loaded, false otherwise. More...
bool | Load (const wxString &name, int flags=wxDL_DEFAULT)
| Loads DLL with the given name into memory. More...
void | Unload ()
| Unloads the library from memory. More...

## Static Public Member Functions
static wxString | GetDllExt (wxDynamicLibraryCategory cat=wxDL_LIBRARY)
| Returns the platform-specific dynamic library file extension, or depending on flags, the plugin file extension. More...
static wxString | CanonicalizeName (const wxString &name, wxDynamicLibraryCategory cat=wxDL_LIBRARY)
| Returns the platform-specific full name for the library called name. More...
static wxString | CanonicalizePluginName (const wxString &name, wxPluginCategory cat=wxDL_PLUGIN_GUI)
| This function does the same thing as CanonicalizeName() but for wxWidgets plugins. More...
static wxDllType | GetProgramHandle ()
| Return a valid handle for the main program itself or nullptr if symbols from the main program can't be loaded on this platform. More...
static wxDynamicLibraryDetailsArray | ListLoaded ()
| This static method returns a vector-like object containing the details of all modules loaded into the address space of the current project. More...
static void * | GetModuleFromAddress (const void *addr, wxString *path=nullptr)
| Returns the load address of the module containing the specified address or nullptr if not found. More...
static void | Unload (wxDllType handle)
| Unloads the library from memory. More...

## Constructor & Destructor Documentation

## ◆ wxDynamicLibrary() [1/2]

wxDynamicLibrary::wxDynamicLibrary | ( |  | ) |

Default constructor.

## ◆ wxDynamicLibrary() [2/2]

wxDynamicLibrary::wxDynamicLibrary | ( | const wxString & | name,
|  | int | flags = `wxDL_DEFAULT`
| ) |  |

Constructor.

Calls Load() with the given name.

## Member Function Documentation

## ◆ Attach()

void wxDynamicLibrary::Attach | ( | wxDllType | h | ) |

Attaches the object to an existing handle.

This allows to give ownership of an existing handle, possibly obtained from Detach(), to this object, so that it will unload it when destroyed.

Since
  3.1.5

## ◆ CanonicalizeName()

static wxString wxDynamicLibrary::CanonicalizeName | ( | const wxString & | name,
|  | wxDynamicLibraryCategory | cat = `wxDL_LIBRARY`
| ) |  |
| static

Returns the platform-specific full name for the library called name.

E.g. it adds a `".dll"` extension under Windows and `"lib"` prefix and `".so"`, `".sl"` or `".dylib"` extension under Unix.

See also
  CanonicalizePluginName()

## ◆ CanonicalizePluginName()

static wxString wxDynamicLibrary::CanonicalizePluginName | ( | const wxString & | name,
|  | wxPluginCategory | cat = `wxDL_PLUGIN_GUI`
| ) |  |
| static

This function does the same thing as CanonicalizeName() but for wxWidgets plugins.

The only difference is that compiler and version information are added to the name to ensure that the plugin which is going to be loaded will be compatible with the main program.

## ◆ Detach()

wxDllType wxDynamicLibrary::Detach | ( |  | ) |

Detaches this object from its library handle.

This means that the object will not unload the library any longer in its destructor but it is now the callers responsibility to do this using static Unload().

See also
  Attach()

## ◆ GetDllExt()

static wxString wxDynamicLibrary::GetDllExt | ( | wxDynamicLibraryCategory | cat = `wxDL_LIBRARY` | ) |
| static

Returns the platform-specific dynamic library file extension, or depending on flags, the plugin file extension.

The leading dot is included.

For example, on Windows `".dll"` is returned, and either `".dylib"` or `".bundle"` on macOS.

## ◆ GetModuleFromAddress()

static void* wxDynamicLibrary::GetModuleFromAddress | ( | const void * | addr,
|  | wxString * | path = `nullptr`
| ) |  |
| static

Returns the load address of the module containing the specified address or nullptr if not found.

If the second argument path is not nullptr, it is filled with the full path to the file the module was loaded from upon a successful return.

This method is implemented under MSW and Unix platforms providing `dladdr()` call (which include Linux and various BSD systems) and always returns nullptr elsewhere.

Since
  3.1.0

## ◆ GetProgramHandle()

static wxDllType wxDynamicLibrary::GetProgramHandle | ( |  | ) |
| static

Return a valid handle for the main program itself or nullptr if symbols from the main program can't be loaded on this platform.

## ◆ GetSymbol()

void* wxDynamicLibrary::GetSymbol | ( | const wxString & | name,
|  | bool * | success = `0`
| ) |  | const

Returns pointer to symbol name in the library or nullptr if the library contains no such symbol.

See also
  wxDYNLIB_FUNCTION()

## ◆ GetSymbolAorW()

void* wxDynamicLibrary::GetSymbolAorW | ( | const wxString & | name | ) | const

This function is available only under Windows as it is only useful when dynamically loading symbols from standard Windows DLLs.

Such functions have either `'A'` (in ANSI build) or `'W'` (in Unicode, or wide character build) suffix if they take string parameters. Using this function, you can use just the base name of the function and the correct suffix is appended automatically depending on the current build. Otherwise, this method is identical to GetSymbol().

Availability: only available for the wxMSW port.

## ◆ HasSymbol()

bool wxDynamicLibrary::HasSymbol | ( | const wxString & | name | ) | const

Returns true if the symbol with the given name is present in the dynamic library, false otherwise.

Unlike GetSymbol(), this function doesn't log an error message if the symbol is not found.

Since
  2.5.4

## ◆ IsLoaded()

bool wxDynamicLibrary::IsLoaded | ( |  | ) | const

Returns true if the library was successfully loaded, false otherwise.

## ◆ ListLoaded()

static wxDynamicLibraryDetailsArray wxDynamicLibrary::ListLoaded | ( |  | ) |
| static

This static method returns a vector-like object containing the details of all modules loaded into the address space of the current project.

The array elements are objects of the type wxDynamicLibraryDetails. Under Unix systems they appear in the order in which they libraries have been loaded, with the module corresponding to the main program itself coming first.

The returned array will be empty if an error occurred or if the function is not implemented for the current platform.

This method is currently implemented only under Win32 and Unix systems providing `dl_iterate_phdr()` function (such as Linux) and is useful mostly for diagnostics purposes.

## ◆ Load()

bool wxDynamicLibrary::Load | ( | const wxString & | name,
|  | int | flags = `wxDL_DEFAULT`
| ) |  |

Loads DLL with the given name into memory.

The flags argument can be a combination of the styles outlined in the class description.

Returns true if the library was successfully loaded, false otherwise.

## ◆ Unload() [1/2]

void wxDynamicLibrary::Unload | ( |  | ) |

Unloads the library from memory.

wxDynamicLibrary object automatically calls this method from its destructor if it had been successfully loaded.

## ◆ Unload() [2/2]

static void wxDynamicLibrary::Unload | ( | wxDllType | handle | ) |
| static

Unloads the library from memory.

wxDynamicLibrary object automatically calls this method from its destructor if it had been successfully loaded.

This version of Unload() is only used if you need to keep the library in memory during a longer period of time than the scope of the wxDynamicLibrary object. In this case you may call Detach() and store the handle somewhere and call this static method later to unload it.