中
中原
Unregistered / Unconfirmed
GUEST, unregistred user!
回调函数的贴子很多,可真正有用的没有几张。我现在要写一个回调函数,我需要的是通过<br>dll来调用Exe中的函数,然后返回结果<br><br>以下是完整的例子,但它只是通过EXE去调用DLL。这和一般的DLL调用没有多大的区别。<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><br>我把上面的代码该写为:<br>DLL部分<br>library Prdllsamp;<br><br><br>uses<br> SysUtils,<br> Classes;<br><br>{$R *.res}<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><br><br>exports<br><br> Myfun index 1;<br>begin<br>end.<br><br><br>调用程序<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TCallBackFun=function(x,y:integer):integer;stdcall;<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> function Myfun(x,y:integer;f:TCallBackFun):integer;stdcall;external 'prdllsamp.dll';<br>{$R *.dfm}<br><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> showmessage(inttostr(result));<br> end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> showmessage(inttostr(MyFun(10,20,@MultiTwo)));<br>end;<br>end.<br><br>功能好象是达到了,但不能用。我对回调函数不熟,请那位高人指点!<br><br>