Now, how is the dynamic loading that you say?
Opa, amigo, isto é a cosa legal do Lazarus-Pascal : Dynamic loading of libraries ...
Dynamic loading of libraries, the must of Lazarus-Pascal...
For that you need to add in use section of mylib.pas :
uses
dynlibsThen add the dynamic load function :
Function Load_mylib(const dllfilename:string) :boolean;
Then create the Vars that will hold our dynamically loaded functions :
var Function1:integer; {$IFDEF windows}stdcall{$ELSE}cdecl{$ENDIF};
and the var that will hold our handle for the dll.
var lib_Handle:TLibHandle; Then in implementation section :
implementation
Function Load_mylib (const dllfilename:string) :boolean;
begin
Result := False;
if lib_Handle<>0 then result:=true {is it already there ?}
else begin {go & load the library}
if Length(dllfilename) = 0 then exit;
lib_Handle:=DynLibs.LoadLibrary(dllfilename); // obtain the handle we want
if lib_Handle <> DynLibs.NilHandle then
begin {now we tie the functions to the VARs from above}
Pointer(Function1) := DynLibs.GetProcedureAddress(lib_Handle,PChar('Function1'));
/// all the other functions...
end;
Do not forget to unload the library with :
Procedure Unload_mylib;
begin
if lib_Handle<>DynLibs.NilHandle then
begin
DynLibs.UnloadLibrary(lib_Handle);
end;
lib_Handle:=DynLibs.NilHandle;
end;In main form1.pas, you can add at formcreate or also in a buttonclick procedure :
Load_mylib('myfolder/mylib.so')So you can load the library when, from where you want and use your mylib functions the same way than for static library.
