procedure TfrmDemo1.bntDynaCallClick(Sender: TObject);
var
HInst:THandle;
FPointer:TFarProc;
MyFunc:TAdd;
begin
HInst:=LoadLibrary(DllName); //调用DLL
if HInst>0 then //如果成功
try
FPointer:=GetProcAddress(HInst,PChar('Add')); //获得函数地址
if FPointer< >nil then
begin
GetVal(FVal1,FVal2);
MyFunc:=TAdd(FPointer);
label5.caption:=inttostr(MyFunc(FVal1,FVal2));
end
else
messagebox(0,'Funtion Add not found','Warning',MB_OK);
finally
FreeLibrary(HInst);
end
else
messagebox(0,'DllName not found','Warning',MB_OK);
end;
添加一个新单元calc.pas,在单元中声明两个函数并实现;
function Add(val1,val2:integer):integer;stdcall;
function Substract(val1,val2:integer):integer;stdcall;
implementation
……
---- 2)在DLL创建类
---- *添加一个新单元UseClass.pas
---- * 定义一个基类
TDllCalc=class
public
function Multiply(val1,val2:integer):integer;virtual;abstract;
function Divide(val1,val2:integer):integer;virtual;abstract;
end;
---- * 定义一派生类并实现其函数
TDllCalcImpl=class(TDllCalc)
public
function Multiply(val1,val2:integer):integer;override;
function Divide(val1,val2:integer):integer;override;
end;
implementation
……
---- * 声明一个实现函数
function ClassCalcImpl:TDllCalc;stdcall;
function ClassCalcImpl:TDllCalc;
begin
result:=TDllCalcImpl.Create;
end;
function Add(val1,val2:integer):integer
;stdcall;external 'dllDemo.dll';
function Substract(val1,val2:integer)
:integer;stdcall;external 'dllDemo.dll';