type
    TFctFoo = function(I: Integer) : integer;  // You need one per function 
signature
var
    HLib: THandle = 0;  // You need one per DLL
    FooPtr : TFctFoo;    // You need one per function

function Foo(I : Integer) : Integer;
begin
    if FooPtr = nil then begin
      if HLib = 0 then
        HLib := LoadLibrary(...);
      FooPtr := TFctFoo(GetProcAddress(HLib,...));
    end;
    Result := FooPtr(I);
end;


--
[EMAIL PROTECTED]
The author of the freeware multi-tier middleware MidWare
The author of the freeware Internet Component Suite (ICS)
http://www.overbyte.be

----- Original Message ----- 
From: "Gunnar Blumert" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Saturday, July 28, 2007 4:00 PM
Subject: Loading DLLs very dynamically


> Hi there,
>
> I have an application that sometimes makes use of a DLL, but usually
> doesn't. Therefore I'd like to load the DLL only if required.
> However, instead of coding a LoadLibrary/GetProcAddress - FreeLibrary 
> around
> each call of a function in the DLL, I had the idea to leave the
> application's code as is and only change the interface-unit that links to
> the DLL as follows:
>
> type TCallFoo = Function(I: integer): integer; stdcall;  // Prototype of 
> the
> function in the DLL
>
> var HLib: THandle = 0;
> Function Foo: TCallFoo;
> begin
>  if HLib = 0 then HLib := LoadLibrary(...);
>  Result := GetProcAddress(HLib,...);
> end;
>
> So far, so good. But in my application when I have the code
>
> var I: integer;
> ...
> I := Foo(5);
>
> I get two error messages. The first one says "Too many parameters", as Foo
> doesn't require any parameters. The second one is "Type mismatch: integer
> and TCallFoo", because the type of the result of Foo is TCallFoo and not
> integer. Of course my intention is to call the result of Foo (which is the
> address of the function in the DLL).
>
> The only way I could find that works is
>
> var I: integer;
>     CallFoo: TCallFoo;
> ...
>  CallFoo := Foo;
>  I := CallFoo(5);
>
> In other words, I had to change the code of my application again. Is there 
> a
> way to avoid the variable CallFoo?
>
> Thanks!
> Gunnar
>
> _______________________________________________
> Delphi mailing list -> [email protected]
> http://www.elists.org/mailman/listinfo/delphi 
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to