[fpc-devel] Dynamically Loading Libraries
Ivo Steinmann
ivo_steinmann at gmx.net
Mon Nov 9 15:23:46 CET 2009
Joost van der Sluis schrieb:
> On Mon, 2009-11-09 at 09:14 +0100, Florian Klaempfl wrote:
>
>> Ivo Steinmann schrieb:
>>
>>> Delphi have got also a solution:
>>>
>>> http://docwiki.embarcadero.com/RADStudio/en/Libraries_and_Packages#Delayed_Loading
>>>
>> Soon or later people will request this anyways I fear ...
>>
>
> MeMeMe! ;)
>
> This time a feature I like, while I do not like things like
> 'for..in' (Because it's not an orthogonal feature, another word I just
> learned in respect to programming)
>
> It's a nightmare to maintain external library-headers when all exported
> functions have to be declared twice. Once for static loading, and one
> for dynamic loading. And the fact that all bindings are really done even
> when they are not used is also very annoying. To solve this adding
> wrappers for each function..... Means that you have to declare each
> exported function three times...
>
> I don't see the problem when the exported function is defined 'static' -
> so a link-error happens when it is not available at compile time, and a
> error occurs when the program is started while the library is not
> available.
>
> Or weak. Which means that a library is loaded when the first call to a
> function is made. Then the error of a missing library/exported function
> can be given on the moment of the first function call. Nothing special
> here?
>
> (In fact the compiler generates the necessary wrapper-function:
> Function AExternalfunction(param1: type1): type2;
> begin
> if not loaded(ExternalLibrary) then
> LoadDynamicLibrary(ExternalLibraryName) // if fails exception
> if not assigned(TempFunctionVariable) then
> @tempFunctionVariable:=GerProcedureAddress(...) // fail->exception
> Result := tempFunctionVariable(param1);
> )
>
> Joost
>
>
>
> _______________________________________________
> fpc-devel maillist - fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
>
>
or the compiler even generates that:
var Externalfunction: Function(param1: type1): type2 := @NotInitialized;
Function NotInitialized(param1: type1): type2;
begin
if not loaded(ExternalLibrary) then
LoadDynamicLibrary(ExternalLibraryName) // if fails exception
Externalfunction := GerProcedureAddress(...) // fail->exception
Result := Externalfunction(param1);
end;
The first call to a function is routed through an initialization
function. All later calls are done directly. This avoids unnecessary
"singleton" checks.
And it would be also nice that it's possible to initialize the library
by the user. So that the user is not forced to check at each library
call, if it fails or not because the library is not available.
So the compiler should also generate a public function
function InitializeLibrary[Name]: Boolean;
or even
function InitializeLibrary[Name](const LibraryName: String): Boolean;
-Ivo Steinmann
More information about the fpc-devel
mailing list