Difference between revisions of "Dynamically loading headers"

From Free Pascal wiki
(Unit naming)
(Implementation (suggestions))
Line 72: Line 72:
 
   procedure ClearLibrarySymbols(const Symbols: PLibSymbol; const Count: Integer);
 
   procedure ClearLibrarySymbols(const Symbols: PLibSymbol; const Count: Integer);
  
=== Unit naming ===
+
=== Naming conventions ===
  
 
* static: someheader.pas
 
* static: someheader.pas
Line 78: Line 78:
  
 
So we just add the suffix "dyn"
 
So we just add the suffix "dyn"
 +
 +
 +
=== Header implementation ===
  
 
==Example==
 
==Example==

Revision as of 01:03, 1 November 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);

Naming conventions

  • static: someheader.pas
  • dynamic: someheaderdyn.pas

So we just add the suffix "dyn"


Header implementation

Example