您的位置:首页 > 其它

DLL的静态调用和动态调用

2014-03-04 07:07 344 查看
// ------------------------------------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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: