SharedLib

SharedLib is an interface to system-specific shared libraries, such as ".dll", ".so" or ".dylib" files. It provides a simple interface to obtain symbol addresses (such as function pointers) from these libraries.

Members

Enums

LoadMode
enum LoadMode

Mapped from RTLD_NOW, RTLD_LAZY, RTLD_GLOBAL and RTLD_LOCAL

Functions

getSymbol
void* getSymbol(const(char)* name)

Obtains the address of a symbol within the shared library

getSymbolNoThrow
void* getSymbolNoThrow(const(char)* name)

Obtains the address of a symbol within the shared library

unload
void unload()

Unloads the OS-specific shared library associated with this SharedLib instance.

unloadNoThrow
void unloadNoThrow()

Unloads the OS-specific shared library associated with this SharedLib instance.

Properties

path
const(char)[] path [@property getter]

Returns the path to the OS-specific shared library associated with this object.

Static functions

load
SharedLib load(const(char)[] path, LoadMode mode)

Loads an OS-specific shared library.

loadNoThrow
SharedLib loadNoThrow(const(char)[] path, LoadMode mode)

Loads an OS-specific shared library.

numLoadedLibs
uint numLoadedLibs()

Returns the total number of libraries currently loaded by SharedLib

Examples

void main() {
    if (auto lib = SharedLib.load(`c:\windows\system32\opengl32.dll`)) {
        Trace.formatln("Library successfully loaded");

        void* ptr = lib.getSymbol("glClear");
        if (ptr) {
            Trace.formatln("Symbol glClear found. Address = 0x{:x}", ptr);
        } else {
            Trace.formatln("Symbol glClear not found");
        }

        lib.unload();
    } else {
        Trace.formatln("Could not load the library");
    }

    assert (0 == SharedLib.numLoadedLibs);
}

This implementation uses reference counting, thus a library is not loaded again if it has been loaded before and not unloaded by the user. Unloading a SharedLib decreases its reference count. When it reaches 0, the shared library associated with it is unloaded and the SharedLib instance is deleted. Please do not delete SharedLib instances manually, unload() will take care of it.

Note: SharedLib is thread-safe.

Meta