| Description | Hierarchy | Fields | Methods | Properties |
type TDynLib = class(TObject)
Load functions from dynamic libraries.
I wrote my own class to handle dynamic libraries because:
I wanted to have Load and Symbol functions that by default do error checking (and raise necessary exceptions).
I wanted to have a field SymbolErrorBehaviour — this lets me to specify, once for all subsequent Symbol calls, what error checking I want. Default is to check errors and raise exceptions. There is also a very usefull value reWarnAndContinue: it allows you to run program once and see all symbols that are missing from dynamic library.
Also, the interface of this is OS-independent and works for both FPC and Delphi, so you can avoid ugly $ifdefs in your code.
Typical usage:
var ALLibrary: TDynLib = nil; initialization ALLibrary := TDynLib.Load('libopenal.so'); { ... some calls to ALLibrary.Symbol() ... } finalization FreeAndNil(ALLibrary); end.
It is important that ALLibrary is initialized to nil (actually, writing " = nil" is not necessary for a global variable) and that in finalization you use Free(AndNil). This allows you to exit gracefully if library does not exist on the system and Load will raise an exception: ALLibrary will stay then as nil and FreeAndNil(ALLibrary) will be a valid NOP. Using FreeAndNil(ALLibrary) instead of ALLibrary.Free is just a good practice.
![]() |
constructor Create(const AName: string; AHandle: TDynLibHandle); |
![]() |
destructor Destroy; override; |
![]() |
class function Load(const AName: string; CheckResult: boolean = true): TDynLib; |
![]() |
function Symbol(SymbolName: PChar): Pointer; |
![]() |
property Name: string read FName; |
![]() |
property SymbolErrorBehaviour: TDynLibSymbolErrorBehaviour
read FSymbolErrorBehaviour write FSymbolErrorBehaviour
default seRaise; |
![]() |
constructor Create(const AName: string; AHandle: TDynLibHandle); |
|
Standard constructor, requires a valid TDynLibHandle already. Usually you will prefer to use Load method instead of directly calling this constructor. Exceptions raised
| |
![]() |
destructor Destroy; override; |
![]() |
class function Load(const AName: string; CheckResult: boolean = true): TDynLib; |
|
Link to a dynamic library specified by Name. Returns created TDynLib instance. If the library is not found and CheckResult is Note that the default situation prevents from unintentionally ignoring an error and that's good. Exceptions raised
| |
![]() |
function Symbol(SymbolName: PChar): Pointer; |
|
Return address of given symbol (function name etc.) from loaded dynamic library. If the symbol doesn't exist, then SymbolErrorBehaviour says what happens:
Exceptions raised
| |
![]() |
property SymbolErrorBehaviour: TDynLibSymbolErrorBehaviour
read FSymbolErrorBehaviour write FSymbolErrorBehaviour
default seRaise; |
|
What happens when Symbol fails. | |