Stuff for working with dynamic libraries.
Native dynamic library handle.
It is HMODULE on Windows and void* on Posix.
This struct encapsulates functionality for working with dynamic libraries.
This struct is neither default constructable nor copyable. Pass it by ref to functions or use std.typecons.RefCounted.
Construct a DynamicLib from a manually obtained DynamicLibHandle.
DynamicLibHandle handle | A valid DynamicLibHandle. |
bool own | Whether or not to close handle on destruction. |
Construct a DynamicLib with the library name.
char[] name | A name of the dynamic library to open. |
bool search | Whether system will search for dynamic library. If false name is expected to be a path for the dynamic library file. |
Returns whether this is associated with a dynamic library handle. It is asserted that no member functions are called for an unassociated DynamicLib struct.
assert(!DynamicLib.init.associated); auto h = DynamicLib.init.handle; // assertion failure
Returns whether handle of the associated dynamic library will be closed on destruction.
Gets native handle of the associated dynamic library.
Returns the address of a symbol named symbolName or null if it is not found.
Returns the address of a symbol named symbolName.
Calls the function named functionName from this dynamic library.
extern(C) alias F = int function(int) nothrow; const int res = dynLib.call!F("f", 5);
Returns the reference to the variable named varName from this dynamic library.
dynLib.var!int("i") = 3; int* j = &dynLib.var!int("j")
Closes native handle and makes the struct unassociated.
Tries to set sym to point to a symbol named symbolName in dynamic library lib.
extern(C) alias F = void function(int) nothrow; F f; if(dynLib.tryBind!f()) f(5);
Sets sym to point to a symbol named symbolName in dynamic library lib.
extern(C) alias F = void function(int) nothrow; F f; dynLib.bind!f(); f(5);
Returns an instance of struct T with all fields pointing to corresponding symbols in dynamic library lib.
version(Windows): struct Kernel32 { extern(Windows) nothrow: alias DWORD = uint; DWORD function() GetVersion, GetTickCount; } const kernel32 = DynamicLib("Kernel32.dll", true).bind!Kernel32(); import std.stdio; writefln("GetVersion: %X", kernel32.GetVersion()); writefln("GetTickCount: %s", kernel32.GetTickCount());