Re: Mapping DLL procedure to Delphi



I wrote a test app to use either a DLL call or a Delphi procedure.
Basically one has variables holding the addresses of either the DLL
procedures or the Delphi procedures, and allocates the variable
appropriately.

Firstly one declares procedures eg ...

var
{Install functions}
hLexInst : THandle;
GetLexSysDirectory : function(var szLexFP : PChar) : DWord; stdcall;
GSMCodecPresent : function : boolean; stdcall;
PCMCodecPresent : function : boolean; stdcall;
CheckKey : function(szCDIdKey : PChar;
iConnectType, iProgInstall : integer) : DWord;
stdcall;
Install : procedure(szDirectory : PChar); stdcall;

Then one gets these values either from the DLL or from the Delphi
procedure. These are my button OnClick event handlers ...

procedure TForm1.UseUnitDLLClick(Sender: TObject);
begin
if (hLexInst <> 0) then begin
FreeLibrary(hLexInst);
hLexInst := 0;
end;
ConnectAsUnit;
end;

procedure TForm1.UseDLLMnuClick(Sender: TObject);
begin
ConnectAsDLL;
end;

procedure TForm1.ConnectAsUnit;
begin
{set procedure addresses to unit functions}
@GetLexSysDirectory := @LexInstallU.GetLexSysDirectory;
@GSMCodecPresent := @LexInstallU.GSMCodecPresent;
@PCMCodecPresent := @LexInstallU.PCMCodecPresent;
@CheckKey := @LexInstallU.CheckKey;
@Install := @LexInstallU.Install;

// etc etc

end;

procedure TForm1.ConnectAsDLL;
begin
{load DLL}
hLexInst := LoadLibrary('LexInstall.dll');
if hLexInst > 32 then begin
{set procedure addresses to DLL functions}
@GetLexSysDirectory := GetProcAddress(hLexInst,
'GetLexSysDirectory');
@CheckKey := GetProcAddress(hLexInst, 'CheckKey');
@Install := GetProcAddress(hLexInst, 'Install');
@GSMCodecPresent := GetProcAddress(hLexInst, 'GSMCodecPresent');
@PCMCodecPresent := GetProcAddress(hLexInst, 'PCMCodecPresent');
end;

// etc etc

end;

Then call the procedures in the usual way by name with parameters.

Alan Lloyd

.


Quantcast