Castle Game Engine: CastleDynLib: Class TDynLib

# Class TDynLib
Description | Hierarchy | Fields | Methods | Properties
## Unit CastleDynLib ## Declaration `type TDynLib = class(TObject)` ## Description 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. ## Hierarchy TObject TDynLib ## Overview ### Methods
| `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;`
### Properties
| `property Name: string read FName;`
| `property SymbolErrorBehaviour: TDynLibSymbolErrorBehaviour read FSymbolErrorBehaviour write FSymbolErrorBehaviour default seRaise;`
## Description ### Methods
| `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 ECheckFailed if you supply invalid handle.
| `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 `False`, `Nil` will be returned. If CheckResult is `True` then EDynLibError will be raised in case library is not found. So if CheckResult is `True`, `Nil` is never returned. Note that the default situation prevents from unintentionally ignoring an error and that's good. ###### Exceptions raised EDynLibError If library not found and CheckResult is `True`.
| `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: seRaise (default), then EDynLibError will be raised. seReturnNil, then return `Nil` (and continue, ignoring error). seWarnAndReturnNil, then write warning (using WarningWrite) and return `Nil` (and continue, ignoring error). This is useful for debugging : you can easily open the library and after one run of the program you can see what symbols (that you requested) were missing from the library. This is useful when you have a library but you are not sure whether it is compatible and contains all the symbols that you want. ###### Exceptions raised EDynLibError If SymbolName doesn't exist and SymbolErrorBehaviour is seRaise.
### Properties
| `property Name: string read FName;`
Name of the library to link to. In practice, file name of the *.so or *.dylib or *.dll file. A precise strategy where this library is searched is specific to a platform, see the semantics of SysUtils.LoadLibrary (DynLibs for FPC) call on given OS.
| `property SymbolErrorBehaviour: TDynLibSymbolErrorBehaviour read FSymbolErrorBehaviour write FSymbolErrorBehaviour default seRaise;`
What happens when Symbol fails.
Generated by PasDoc 0.13.0 on 2015-06-15 04:43:09