Hello.
Because of the maturity and recent optimization of fpc, there is no more reason that nearly all the good shared libraries are in C.
fpc libraries can do all what c libraries do, but even more...
For example, a C library can not dynamically load other libraries.
fpc can.
C libraries are system-specific while fpc libraries can be used in lot of systems, with the same code.
I went to several foreign-languge fora asking help to translate a Pascal header-type but with no luck... (C,C++, Java, Python,... no answers...)
So i will try here...
That piece of code is the "classical-simple" way to dynamically load a external library:
unit mylib_h /// the pascal header
interface
uses
DynLibs;
type
TProc = procedure ;
var
MyFunctionStr: function(Str: String): String; cdecl;
MyFunctionInt: function(Int: Integer): Integer; cdecl;
MyFunctionCar: function(Car: Cardinal): Cardinal; cdecl;
MyFunctionMulti : function(Car: cardinal; Str: String; Proc: Tproc): Integer; cdecl;
MyProc: procedure() ; cdecl;
MyProcArg: procedure(Car: cardinal); cdecl;
MyProcMulti : procedure(Car: cardinal; Str: String; Proc: Tproc); cdecl;
MyFunctionInit : function(str : String) : boolean ;
MyFunctionEnd : function() : boolean ;
function MyLibLoad(MylibFileName: String ; Ainit: string): integer;
procedure MyLibUnload() ;
implementation
function MyLibLoad(MylibFileName: String; Ainit : string): boolean;
begin
Result := False;
LibHandle := DynLibs.LoadLibrary(MylibFileName);
if LibHandle <> DynLibs.NilHandle then
begin
Pointer(MyFunctionStr) := GetProcAddress(LibHandle, 'MyFunctionStr');
Pointer(MyFunctionInt) := GetProcAddress(LibHandle, 'MyFunctionInt');
Pointer(MyFunctionCar) := GetProcAddress(LibHandle, 'MyFunctionCar');
Pointer(MyFunctionMulti) := GetProcAddress(LibHandle, 'MyFunctionStr');
Pointer(MyProc) := GetProcAddress(LibHandle, 'MyProc');
Pointer(MyProcArg) := GetProcAddress(LibHandle, 'MyProcArg');
Pointer(MyProcMulti) := GetProcAddress(LibHandle, 'MyProcMulti');
Pointer(MyFunctionInit) := GetProcAddress(LibHandle, 'MyFunctionInit');
Pointer(MyFunctionEnd) := GetProcAddress(LibHandle, 'MyFunctionEnd');
result := MyFunctionInit(Ainit) ;
end;
end;
procedure MyLibUnload();
begin
MyFunctionEnd();
if LibHandle <> DynLibs.NilHandle then
begin
DynLibs.UnloadLibrary(LibHandle);
LibHandle := DynLibs.NilHandle;
end;
end;
I will be very happy is somebody could translate that code into C, Java, Python, Basic,...
And it will be very useful for other developers too.
Many thanks.