Difference between revisions of "Dynamically loading headers"

From Free Pascal wiki
(Framework)
(Framework)
Line 20: Line 20:
 
type
 
type
 
     PLibHandler = ^TLibHandler;
 
     PLibHandler = ^TLibHandler;
 
+
   
 
     TLibEventLoading = function(User: Pointer; Handler: PLibHandler): Boolean;
 
     TLibEventLoading = function(User: Pointer; Handler: PLibHandler): Boolean;
 
     TLibEventUnloading = procedure(User: Pointer; Handler: PLibHandler);
 
     TLibEventUnloading = procedure(User: Pointer; Handler: PLibHandler);
 
+
   
 
     PPLibSymbol = ^PLibSymbol;
 
     PPLibSymbol = ^PLibSymbol;
 
     PLibSymbol = ^TLibSymbol;
 
     PLibSymbol = ^TLibSymbol;
Line 31: Line 31:
 
       weak: Boolean;  { weak }
 
       weak: Boolean;  { weak }
 
     end;
 
     end;
 
+
   
 
     TLibHandler = record
 
     TLibHandler = record
 
       InterfaceName: String;                { abstract name of the library }
 
       InterfaceName: String;                { abstract name of the library }

Revision as of 23:59, 31 October 2009

x

Abstract

Currently, most library headers in fpc/packages support only static linking. Some exceptions are mysql, ibase, sqlite (and some other database headers). The idea is now to add dynamic linking support for other headers. This page should provide some place to list pro/contra, implementation details, etc...

Since dynamic linking is not supported by the compiler directly, it's required to define some framework, so that all headers are "implemented" in an uniform way and can be used easily.

Pro

  • loading libraries at runtime (plugin support)

Contra

Implementation (suggestions)

Framework

(dynlibs.pas) type

   PLibHandler = ^TLibHandler;
   
   TLibEventLoading = function(User: Pointer; Handler: PLibHandler): Boolean;
   TLibEventUnloading = procedure(User: Pointer; Handler: PLibHandler);
   
   PPLibSymbol = ^PLibSymbol;
   PLibSymbol = ^TLibSymbol;
   TLibSymbol = record
     pvar: PPointer;  { pointer to Symbol variable }
     name: String;    { name of the Symbol }
     weak: Boolean;   { weak }
   end;
   
   TLibHandler = record
     InterfaceName: String;                { abstract name of the library }
     Defaults     : array of String;       { list of default library filenames }
     Filename     : String;                { handle of the current loaded library }
     Handle       : TLibHandle;            { filename of the current loaded library }
     Loading      : TLibEventLoading;      { loading event, called after the unit is loaded }
     Unloading    : TLibEventUnloading;    { unloading event, called before the unit is unloaded }
     SymCount     : Integer;               { number of symbols }
     Symbols      : PLibSymbol;            { symbol address- and namelist }
     ErrorMsg     : String;                { last error message }
     RefCount     : Integer;               { reference counter }
   end;


 { handler definition }
 function LibraryHandler(const InterfaceName: String; const DefaultLibraries: array of String;
   const Symbols: PLibSymbol; const SymCount: Integer; const AfterLoading: TLibEventLoading = nil;
   const BeforeUnloading: TLibEventUnloading = nil): TLibHandler;
 { initialization/finalization }
 function TryInitializeLibrary(var Handler: TLibHandler; const LibraryNames: array of String;
   const User: Pointer = nil; const NoSymbolErrors: Boolean = False): Integer;
 function TryInitializeLibrary(var Handler: TLibHandler; const LibraryName: String = ;
   const User: Pointer = nil; const NoSymbolErrors: Boolean = False): Integer;
 function InitializeLibrary(var Handler: TLibHandler; const LibraryNames: array of String;
   const User: Pointer = nil; const NoSymbolErrors: Boolean = False): Integer;
 function InitializeLibrary(var Handler: TLibHandler; const LibraryName: String = ;
   const User: Pointer = nil; const NoSymbolErrors: Boolean = False): Integer;
 function ReleaseLibrary(var Handler: TLibHandler; User: Pointer = nil): Integer;
 { errors }
 procedure AppendLibraryError(var Handler: TLibHandler; const Msg: String);
 function GetLastLibraryError(var Handler: TLibHandler): String;
 procedure RaiseLibraryException(var Handler: TLibHandler);
 { symbol load/clear }
 function LoadLibrarySymbols(const Lib: TLibHandle; const Symbols: PLibSymbol; const Count: Integer;
   const ErrorSym: PPLibSymbol = nil): Boolean;
 procedure ClearLibrarySymbols(const Symbols: PLibSymbol; const Count: Integer);

Example