高分求解,关于DLL回调函数,请各位高手帮忙! ( 积分: 200 )

  • 主题发起人 主题发起人 taizhi
  • 开始时间 开始时间
T

taizhi

Unregistered / Unconfirmed
GUEST, unregistred user!
有一DLL代码如下:<br>library&nbsp;DLL1;<br>uses<br>&nbsp;&nbsp;SysUtils,<br>&nbsp;&nbsp;Classes;<br><br>type<br>&nbsp;&nbsp;TCB1=function&nbsp;(x:Integer):Integer;stdcall;<br><br>{$R&nbsp;*.res}<br><br>function&nbsp;CBDll1(InitValue:Integer;TCB:TCB1):Integer;stdcall;<br>var<br>&nbsp;&nbsp;i:Integer;<br>begin<br>&nbsp;&nbsp;for&nbsp;i:=1&nbsp;to&nbsp;100&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Inc(InitValue);<br>&nbsp;&nbsp;&nbsp;&nbsp;TCB(InitValue);<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;Result:=InitValue;<br>end;<br><br>exports<br>&nbsp;&nbsp;CBDll1;<br><br>begin<br>end.<br><br>有一EXE代码如下:<br>unit&nbsp;Main;<br>interface<br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls,&nbsp;Buttons;<br>type<br>&nbsp;&nbsp;TCB1=function&nbsp;(x:Integer):Integer;&nbsp;<br><br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;Button1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;Memo1:&nbsp;TMemo;<br>&nbsp;&nbsp;&nbsp;&nbsp;BitBtn1:&nbsp;TBitBtn;<br>&nbsp;&nbsp;&nbsp;&nbsp;Memo2:&nbsp;TMemo;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;Button1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;BitBtn1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>function&nbsp;abc(x:Integer):Integer;stdcall;<br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br><br>implementation<br><br>function&nbsp;CBDll1(InitValue:Integer;TCB:TCB1):Integer;stdcall;external&nbsp;'Dll1.dll';<br><br>{$R&nbsp;*.dfm}<br><br>function&nbsp;abc(x:Integer):Integer;stdcall;<br>begin<br>&nbsp;&nbsp;Form1.Memo1.Lines.Add(Inttostr(x));<br>end;<br><br>procedure&nbsp;TForm1.Button1Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;Memo1.Lines.Clear;<br>&nbsp;&nbsp;CBDll1(1,@abc);<br>end;<br>end.<br>我现在用EXE直接调用DLL中的回调函数没有问题,我想在想做到的是,我想把调用DLL1的代码写到DLL2(也是一DLL文件)中,通过调用DLL2来调用DLL1,并且能把结果显示在EXE文件中的MEMO2中,效果要与直接调用DLL1一样,即能获取到每个变化值.这种问题如何解决?
 
不知我的问题说清楚了没有呢?
 
我这样做的目的是由于一个项目有一个需求,需要写一个通用接口程序,所以,必需把调用代码单独写在一个DLL(即DLL2)中,主程序再调用该DLL2
 
这个当然是可以的。就如同你现在调用一样,只不过是写在dll2中
 
appfirst<br>那请问,DLL2中该如何写代码呢?我要的是,把一个InitValue的变化后的值,都返回到EXE程序中去?
 
我要的是在EXE中同步显示InitValue的每一步变化后的值<br>假如EXE程序中,有一Button2及MEMO2,我想在单击Button2后,在memo2中显示所有变化后的InitValue的值,即像Button1的单击事件一样!要同步显示哦!
 
怎么就没有人说两句呢?顶者有分!!!
 
主要的问题是在dll2中调用dll1的同时,还要更新EXE里的memo控件内容是吧.你在EXE里调用的时候把MEMO控件的句柄传给dll2,然后由它进行更改可不可以?楼主试试.
 
DLL函数:function&nbsp;CBDll1(InitValue:Integer;TCB:TCB1):Integer;stdcall;<br>你改成:<br>var<br>type<br>ptcb&nbsp;=&nbsp;^tcb1;<br>function&nbsp;CBDll1(InitValue:Integer;TCB:ptcb):Integer;stdcall;
 
给你一个调试成功的代码,按你的改的.<br>dll1的:<br>library&nbsp;dll1;<br>uses<br>&nbsp;&nbsp;SysUtils,<br>&nbsp;&nbsp;Classes,<br>&nbsp;&nbsp;Windows;<br>type<br>&nbsp;&nbsp;TCB1=function&nbsp;(x:Integer;memo:LPARAM):Integer;stdcall;<br>{$R&nbsp;*.res}<br>function&nbsp;CBDll1(InitValue:Integer;TCB:TCB1;memo:LPARAM):Integer;stdcall;<br>var<br>&nbsp;&nbsp;i:Integer;<br>begin<br>&nbsp;&nbsp;for&nbsp;i:=1&nbsp;to&nbsp;100&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Inc(InitValue);<br>&nbsp;&nbsp;&nbsp;&nbsp;TCB(InitValue,memo);<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;Result:=InitValue;<br>end;<br>exports<br>&nbsp;&nbsp;CBDll1;<br>begin<br>end.<br>=============<br>dll2的;<br>library&nbsp;dll2;<br>uses<br>&nbsp;&nbsp;SysUtils,<br>&nbsp;&nbsp;Classes,<br>&nbsp;&nbsp;StdCtrls,<br>&nbsp;&nbsp;Windows;<br>type<br>&nbsp;&nbsp;TCB1=function&nbsp;(x:Integer;memo:LPARAM):Integer;stdcall;<br>{$R&nbsp;*.res}<br>function&nbsp;CBDll1(InitValue:Integer;TCB:TCB1;memo:LPARAM):Integer;stdcall;external&nbsp;'dll1.dll';<br>function&nbsp;abc(x:Integer;memo:LPARAM):Integer;stdcall;<br>var<br>&nbsp;&nbsp;mem:TMemo;<br>begin<br>&nbsp;&nbsp;mem:=TMemo(memo);<br>&nbsp;&nbsp;if&nbsp;mem&lt;&gt;nil&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;Mem.Lines.Add(Inttostr(x));<br>end;<br>procedure&nbsp;loadDll1(InitValue:Integer;memo:LPARAM);stdcall;<br>begin<br>&nbsp;&nbsp;CBDll1(InitValue,@abc,memo);<br>end;<br>exports&nbsp;loadDll1;<br>begin<br>end.<br>=================<br>exe的;<br>unit&nbsp;Unit1;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;Messages,&nbsp;SysUtils,&nbsp;Variants,&nbsp;Classes,&nbsp;Graphics,&nbsp;Controls,&nbsp;Forms,<br>&nbsp;&nbsp;Dialogs,&nbsp;StdCtrls;<br><br>type<br>&nbsp;&nbsp;TForm1&nbsp;=&nbsp;class(TForm)<br>&nbsp;&nbsp;&nbsp;&nbsp;mmo1:&nbsp;TMemo;<br>&nbsp;&nbsp;&nbsp;&nbsp;btn1:&nbsp;TButton;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;btn1Click(Sender:&nbsp;TObject);<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Private&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;Public&nbsp;declarations&nbsp;}<br>&nbsp;&nbsp;end;<br><br>var<br>&nbsp;&nbsp;Form1:&nbsp;TForm1;<br><br>implementation<br><br>{$R&nbsp;*.dfm}<br><br>procedure&nbsp;loadDll1(InitValue:Integer;memo:LongInt);stdcall;external&nbsp;'dll2.dll';<br>procedure&nbsp;TForm1.btn1Click(Sender:&nbsp;TObject);<br>begin<br>&nbsp;&nbsp;loadDll1(1,LPARAM(mmo1));<br>end;<br>end.
 
当然,你也可以把abc放到exe里.然后再把地址指针传到dll里.相应的函数就多加一个参数.
 
谢谢各位,我想到一个更好的解决方法了!
 
后退
顶部