以下是完整的例子:
DLL 程序
library Project2;
uses
SysUtils,
Classes;
function AddTwo(x,y:integer):integer;stdcall;
begin
Result:=x+y;
end;
function MultiTwo(x,y:integer):integer;stdcall;
begin
Result:=x*y;
end;
{$R *.RES}
exports AddTwo,MultiTwo;
begin
end.
调用程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function AddTwo(x,y:integer):integer;stdcall;external 'project2.dll';
function MultiTwo(x,y:integer):integer;stdcall;external 'project2.dll';
type
TCallBackFun=function(x,y:integer):integer;stdcall;
function Myfun(x,y:integer;f:TCallBackFun):integer;
begin
result:=TCallBackFun(f)(x,y);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(inttostr(MyFun(10,20,@AddTwo)));
showmessage(inttostr(MyFun(10,20,@MultiTwo)));
end;
end.