TIP: Click on subject to list as thread! ANSI
echo: delphi
to: MICHAEL JOSEPHSON
from: LARS SKOVLUND
date: 1997-07-06 16:40:00
subject: Calling a function in a user defined DLL16:40:2107/06/97

Hello Michael!
Sunday June 29 1997, Michael Josephson writes to All:
 MJ> In a Delphi program is possible to pick the DLL to call a function
 MJ> from at runtime? This is a question that's been bugging me for a while
 MJ> because there's a number of reasons why it would be useful.
 MJ> Here's an example which /should/ make things clearer:
 MJ> Say you've got an email program, you could have a function
 MJ> GetEMailHeader which returns header information for a message and you
 MJ> could have DLLs: HUDSON.DLL, JAM.DLL, NEWMBASE.DLL. If each DLL had a
 MJ> GetEMailHeader function and you could pick whatever message base
 MJ> format you wanted just by telling the program the DLL to use. Other
 MJ> people could write new message base DLLs later.
I don't know whether you have Delphi 1 or 2 (32-bit programs and DLLs can't 
exchange var-parameters, AFAIK) - but if you use Delphi 2 you'd have to 
exchange info in some other way.
Let's assume you have this function declaration in a DLL (delphi 1):
function GetEMailHeader(var f : file;var HeaderInfo : TSomeType) : 
boolean;export;
Then you could access it like (in the main program):
procedure GetEMailHeader(DLL : String;var f : file;var HeaderInfo : 
TSomeType);
type TGetEmailHeader = function(var f : file;var HeaderInfo : TSomeType) : 
boolean; { This _MUST_ match the declaration in the DLL }
var Lib : THandle;
    P : PChar;
    Proc : Pointer;
begin
getmem(p,256);
strpcopy(p,dll);        { Convert DLL to a null-terminated string, just to 
please LoadLibrary }
Lib:=LoadLibrary(P);    { Try to load the DLL }
freemem(p,256);
if Lib<32 then exit;        { An error occurred }
Proc:=GetProcAddress(Lib,'GetEMailHeader');  { Get the address of our 
GetEMailHeader function }
if Proc=nil then exit;      { The DLL does not have a GetEMailHeader function 
}
TGetEmailHeader(Proc^)(f,HeaderInfo); { And call it - note the syntax here }
FreeLibrary(Lib);       { Free the library }
end;
Lars
--- GoldED/386 2.50 UNREG
---------------
* Origin: Yet another T.P.I. Point (2:238/64.46)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.