500分求解(一经通过立即放分)--还是关于回调函数 (300分)

  • 主题发起人 主题发起人 中原
  • 开始时间 开始时间

中原

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>&nbsp; SysUtils,<br>&nbsp; Classes;<br>&nbsp; function AddTwo(x,y:integer):integer;stdcall;<br>&nbsp; begin<br>&nbsp; &nbsp; Result:=x+y;<br>&nbsp; end;<br>&nbsp; function MultiTwo(x,y:integer):integer;stdcall;<br>&nbsp; begin<br>&nbsp; &nbsp; Result:=x*y;<br>&nbsp; end;<br>{$R *.RES}<br>&nbsp; exports AddTwo,MultiTwo;<br>begin<br>end.<br><br><br><br>调用程序<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; 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>&nbsp; TCallBackFun=function(x,y:integer):integer;stdcall;<br><br>function Myfun(x,y:integer;f:TCallBackFun):integer;<br>begin<br>&nbsp; result:=TCallBackFun(f)(x,y);<br>end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; showmessage(inttostr(MyFun(10,20,@AddTwo)));<br>&nbsp; showmessage(inttostr(MyFun(10,20,@MultiTwo)));<br>end;<br><br>end.<br><br>我把上面的代码该写为:<br>DLL部分<br>library Prdllsamp;<br><br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes;<br><br>{$R *.res}<br>type<br>&nbsp; TCallBackFun=function(x,y:integer):integer;stdcall;<br><br>&nbsp; function Myfun(x,y:integer;f:TCallBackFun):integer;<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp; result:=TCallBackFun(f)(x,y);<br>&nbsp; end;<br><br><br>exports<br><br>&nbsp; &nbsp; Myfun &nbsp;index 1;<br>begin<br>end.<br><br><br>调用程序<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TCallBackFun=function(x,y:integer):integer;stdcall;<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>&nbsp;function Myfun(x,y:integer;f:TCallBackFun):integer;stdcall;external 'prdllsamp.dll';<br>{$R *.dfm}<br><br>&nbsp;function AddTwo(x,y:integer):integer;stdcall;<br>&nbsp; begin<br>&nbsp; &nbsp; Result:=x+y;<br>&nbsp; end;<br>&nbsp; function MultiTwo(x,y:integer):integer;stdcall;<br>&nbsp; begin<br>&nbsp; &nbsp; Result:=x*y;<br>&nbsp; &nbsp; showmessage(inttostr(result));<br>&nbsp; end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; showmessage(inttostr(MyFun(10,20,@MultiTwo)));<br>end;<br>end.<br><br>功能好象是达到了,但不能用。我对回调函数不熟,请那位高人指点!<br><br>
 
我觉得你应该将Exe中的公用函数也写成Dll,然后用Exe调用Dll,而以上问题也可转换为<br>Dll调用Dll了。
 
library Prdllsamp;<br><br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes;<br><br><br>type<br>&nbsp; TCallBackFun = function(x, y: integer): integer; stdcall;<br><br>function Myfun(x, y: integer; f: TCallBackFun): Integer; stdcall;<br>begin<br>&nbsp; Result := f(x, y);<br>end;<br><br>exports<br>&nbsp; Myfun;<br><br>begin<br>end.<br><br><br><br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>function Myfun(x, y: integer; f: Pointer): integer; stdcall; external 'project2.dll';<br><br>implementation<br><br>{$R *.dfm}<br><br>function AddTwo(x, y: integer): Integer;stdcall;<br>begin<br>&nbsp; Result := x + y;<br>end;<br><br>function MultiTwo(x, y: integer): Integer;stdcall;<br>begin<br>&nbsp; Result := x * y;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; Label1.Caption := inttostr(MyFun(10, 20, @MultiTwo));<br>end;<br><br>end.<br>
 
不明白为什么要从DLL回调EXE<br><br>[:D][:D][:D][:D][:D]
 
把所有该死的stdcall全部去掉就可以啦
 
  不能用是為什么?我試了一下,可行
 
To joken2000:<br>&nbsp; &nbsp; 你用的可能是上我原来的调用的方法,我希望的方法回出错,而且根本就不能得出正确得结果。<br><br>To 小小鹰:<br>&nbsp; &nbsp; 我需要的是回调函数,你可以参看有关回调函数的例子和说明。<br><br>To sofox:<br>&nbsp; &nbsp; 谢谢,好像可以。可我现在要用的是用调用VC的动态库,我该怎样去做呢?<br><br>To 远帆:<br>&nbsp; &nbsp; 你的答案我能在网上找到很多很多,而且你的这中说法没有实质性的结果.<br>
 
看到你的话我简直是想骂你一顿!!!<br>什么叫我的答案能在网上找到很多很多?这是我在你的程序的基础上改的不是在网上抄的!<br>我这种说法没有实质性的结果?这是什么话?你试试能不能得到结果再说话!
 
to 远帆:<br>&nbsp; &nbsp;呵呵,回的挺快。我试一下,你给的结果很好.现在放分!给个mail以后多联系。
 
多人接受答案了。
 
你声明的回调函数可能有问题<br><br>&nbsp;TCallBackFun=function(x,y:integer):integer;stdcall;<br><br>&nbsp; &nbsp;如果在DLL中,所有的DLL程序共享一块内存区,所以不管[段地址]<br>是什么,只要[偏移地址]相同,都指向同一物理存储单元,如果在EXE<br>中,不同的段地址,同一个[偏移地址]指向的是不同的物里存储单元.<br>所以DLL-&gt;DLL没有问题,DLL-&gt;EXE会出现问题;<br>在C中有只要声明成 CallBack函数就成,CallBack函数肯定是 far 调用<br>所以你出问题在地方是函数声明上
 
你这样试一下<br>TCallBackFun=function(x,y:integer):integer;stdcall;far;<br><br>
 
后退
顶部