Board index » delphi » Running a function in a dynamic DLL

Running a function in a dynamic DLL

Hello,

Does anyone have an example of how to run a function (not a procedure)
in a dynamically loaded DLL (using LoadLibrary)?

Thanks

Ramzi

 

Re:Running a function in a dynamic DLL


Quote
Ramzi Baroody wrote:

> Hello,

> Does anyone have an example of how to run a function (not a procedure)
> in a dynamically loaded DLL (using LoadLibrary)?

> Thanks

> Ramzi

Load the library:

type TGetInfo = function(Info: PChar): integer;
var  hLibrary: THandle;
     fpGetInfo: TFarProc;

First load the library

  hLibrary := LoadLibrary('MYLIB.DLL');  
  if hLibrary < HINSTANCE_ERROR then
    ShowMessage(Format('LoadLibrary failed with error code %d.', [hLibrary]))

then find function's address

    fpGetInfo := GetProcAddress(hLibrary, 'GetInfo');
    if fpGetInfo = nil then
      ShowMessage('Unable to find external GetInfo.');

call you function

    RetValue := TGetInfo(fpGetInfo)(szInfo);
    if RetValue = 0 then
      ShowMessage('GetInfo successful.')
    else
      ShowMessage(Format('GetInfo failed with error code %d', [RetValue]));

and finally unload the library.

    FreeLibrary(hLibrary);

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Ari Hirviniemi, Lappeenranta, Finland (ari.hirvini...@ktieto.fi)
All my opinions and comments are personal and have nothing to do
with my employer.

Re:Running a function in a dynamic DLL


In article <4f5trh$...@sam.inforamp.net>, rbaro...@norm.inforamp.net
says...

Quote

>Hello,

>Does anyone have an example of how to run a function (not a procedure)
>in a dynamically loaded DLL (using LoadLibrary)?

>Thanks

>Ramzi

Hi!

Let's say the function in the dll is declared:

  Function MyTestFn (AParam: LongINt): LongINt;

and the Index of the function is 99

VAR MyTestFn : Function (AParam: LongINt): LongINt;
    hMyDLL   : THandle;
begin
  hMyDLL := LoadLibrary ('MYDLL.DLL');  {Load the lib}
  If hMyDLL >= 32 then {all ok?}
  begin
    @MyTestFn := GetProcAddress (hMyDLL, PChar(99)); {Get the address}
    result := MyTestFunction (4711);  {call the function}
    FreeLibrary (hMyDLL); {Free the lib}
  end;
end;

Bye

Andreas  

--
=============================================
helicon software development - Andreas Prucha
Baden/Vienna, Austria - heli...@helicon.co.at
=============================================

Other Threads