DLL的静态调用和动态调用
// ------------------------------------DLL源代码 circle.dproj -------------------------------------
library circle;
uses
SysUtils,
Classes,
Math;
{$R *.res}
function CircleArea(const radius : double) : double; stdcall;
begin
result := radius * radius * PI;
end;
exports CircleArea;
begin
end.
// ------------------------------------调用DLL--------------------------------------------------
var
Form1: TForm1;
function CircleArea(const radius : double) : double; external 'circle.dll'; // 静态调用
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('static: ' + FormatFloat(',.00',CircleArea(StrToFloat(Edit1.Text))));
end;
procedure TForm1.Button2Click(Sender: TObject);
type
TCircleAreaFunc = function (const radius: double) : double; stdcall;
var
dllHandle : cardinal;
circleAreaFunc : TCircleAreaFunc;
begin
dllHandle := LoadLibrary('circle.dll'); // 动态调用
if dllHandle <> 0 then
begin
@circleAreaFunc := GetProcAddress(dllHandle, 'CircleArea');
if Assigned (circleAreaFunc) then
ShowMessage('dynamic: ' + FormatFloat(',.00',circleAreaFunc(StrToFloat(Edit1.Text))))
else
ShowMessage('"CircleArea" function not found');
FreeLibrary(dllHandle); // 释放动态库
end
else
begin
ShowMessage('circle.dll not found / not loaded');
end;
end;