《Delphi6开发人员指南》中讲到dll开发的一种模式,感觉很受益,特自己写了个dll加以练习。
实现功能的dll源码:
library DllWithInterfaceExample;
{$DEFINE DWILIB}
uses
SysUtils,
Classes,
DllInterface in 'DllInterface.pas';
//实现dll的接口单元
{$R *.res}
//建立自己的函数
function GetMax(NumRec:TNumRec):Integer;stdcall;
begin
if NumRec.NumA>NumRec.NumB then
Result:=NumRec.NumA
else
Result:=NumRec.NumB
end;
//对外公布接口,提供函数名就可以
exports
GetMax;
begin
end.
实现dll接口的源码:
unit DllInterface;
//DllWithInterfaceExaple.dll的接口
interface
type //其他程序调用时须提供的数据类型
TNumRec=record
NumA,
NumB:Integer
end;
{$IFNDEF DWILIB} //其他程序调用时须列出函数声明
function GetMax(NumRec:TNumRec):Integer;stdcall;
{$ENDIF}
implementation
{$IFNDEF DWILIB} //然后指出要调用的dll名和函数名
function GetMax(NumRec:TNumRec):Integer;external 'DllWithInterfaceExample.dll' name 'GetMax';
{$ENDIF}
end.
dll发布后同时提供dll接口的源码,便于别人的调用,也方便自己修改。