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