请教关于DLL的进口函数和出口函数的问题? ( 积分: 100 )

  • 主题发起人 主题发起人 chengang_1981
  • 开始时间 开始时间
C

chengang_1981

Unregistered / Unconfirmed
GUEST, unregistred user!
我最近看了点关于DLL进出口函数的资料,,自己写了个DLL,,但是编译不能通过,请问各位大虾问题在哪儿?<br>library Project1;<br>{ Important note about DLL memory management: ShareMem must be the<br> &nbsp;first unit in your library's USES clause AND your project's (select<br> &nbsp;Project-View Source) USES clause if your DLL exports any procedures or<br> &nbsp;functions that pass strings as parameters or function results. This<br> &nbsp;applies to all strings passed to and from your DLL--even those that<br> &nbsp;are nested in records and classes. ShareMem is the interface unit to<br> &nbsp;the BORLNDMM.DLL shared memory manager, which must be deployed along<br> &nbsp;with your DLL. To avoid using BORLNDMM.DLL, pass string information<br> &nbsp;using PChar or ShortString parameters. }<br><br>uses<br> &nbsp;SysUtils,<br> &nbsp;Classes,<br> &nbsp;dialogs;<br><br>{$R *.res}<br>procedure into;<br>begin<br>showmessage('进入');<br>end;<br>procedure outo;<br>begin<br>showmessage('退出');<br>end;<br>initialization<br>into;<br>finalization<br>outo;<br>begin<br>end.<br><br><br>在请教一个问题,,就是在钩子的回调函数里向外发送的消息,对应的窗口是收不到这个消息的,对吗?
 
我最近看了点关于DLL进出口函数的资料,,自己写了个DLL,,但是编译不能通过,请问各位大虾问题在哪儿?<br>library Project1;<br>{ Important note about DLL memory management: ShareMem must be the<br> &nbsp;first unit in your library's USES clause AND your project's (select<br> &nbsp;Project-View Source) USES clause if your DLL exports any procedures or<br> &nbsp;functions that pass strings as parameters or function results. This<br> &nbsp;applies to all strings passed to and from your DLL--even those that<br> &nbsp;are nested in records and classes. ShareMem is the interface unit to<br> &nbsp;the BORLNDMM.DLL shared memory manager, which must be deployed along<br> &nbsp;with your DLL. To avoid using BORLNDMM.DLL, pass string information<br> &nbsp;using PChar or ShortString parameters. }<br><br>uses<br> &nbsp;SysUtils,<br> &nbsp;Classes,<br> &nbsp;dialogs;<br><br>{$R *.res}<br>procedure into;<br>begin<br>showmessage('进入');<br>end;<br>procedure outo;<br>begin<br>showmessage('退出');<br>end;<br>initialization<br>into;<br>finalization<br>outo;<br>begin<br>end.<br><br><br>在请教一个问题,,就是在钩子的回调函数里向外发送的消息,对应的窗口是收不到这个消息的,对吗?
 
工程文件哪有initialization和finalization部分?可以添加一个Unit,带上initialization和finalization即可
 
多谢:lichengbin,第一个问题已经解决,结贴时一定50分送上。<br>请各位大虾在给我说说第二个问题?<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;谢先了
 
第二个问题,收得到消息。
 
下面是个实验程序,,就是当钩子截获到第一个消息时就给主窗体发一个WM_COPYDATA消息,但问题是主程序一点反映都没有,,应该是根本就没收到这个消息,,请大虾们解释解释这个问题的原因。<br>dll部分::::<br>library Project1;<br>uses<br> &nbsp;SysUtils,<br> &nbsp;Classes,<br> &nbsp;Windows,<br> &nbsp;Messages;<br><br>{$R *.RES}<br>var<br>HookHandle:HHook;<br>dfhandle:thandle;<br>isbool:boolean;<br>function TestHookProc(Code:Integer;WParam:WParam;Msg:LongInt):LRESULT;stdcall;<br>begin<br>if isbool then<br>SendMessage(dfhandle,WM_COPYDATA,0,0);<br>isbool:=false;<br>Result:=CallNextHookEx(HookHandle,Code,WParam,longint(@Msg));<br>end;<br><br>procedure HookOn(tID,fhandle:DWord);stdcall;<br>begin<br>HookHandle:=SetWindowsHookEx(WH_GETMESSAGE,TestHookProc,HInstance,0);<br>dfhandle:=fhandle;<br>isbool:=true;<br>end;<br>procedure HookOff;stdcall;<br>begin<br>UnHookWindowsHookEx(HookHandle);<br>end;<br>exports<br>HookOn,<br>HookOff;<br>begin<br>end.<br>主程序部分:<br>unit Unit1;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> &nbsp;StdCtrls, ExtCtrls;<br>type<br> &nbsp;TForm1 = class(TForm)<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br>procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br>private<br> &nbsp;procedure WMCopydata(var message:TMessage);message WM_COPYDATA;<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br> &nbsp;THookEndProc=procedure;stdcall;<br> &nbsp;THookStartProc=procedure(tID,f:DWord);stdcall;<br>var<br> &nbsp;Form1: TForm1;<br> &nbsp;handdll:THandle;<br>implementation<br>{$R *.DFM}<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>HookOn:THookStartProc;<br>begin<br>handdll:=LoadLibrary('./DLL/Project1.dll');<br>HookOn:=GetProcAddress(handdll,'HookOn');<br>HookOn(tId,Form1.Handle);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>HookOff:THookEndProc;<br>begin<br>HookOff:=GetProcAddress(handdll,'HookOff');<br>HookOff;<br>FreeLibrary(handdll);<br>end;<br>procedure TForm1.WMCopydata(var message:TMessage);<br>begin<br>showmessage('收到');<br>end;<br>end.
 
求求你们,大虾们说说啊,
 
这个问题,你跟踪调一下结了,没办法帮的<br>if isbool then<br>SendMessage(dfhandle,WM_COPYDATA,0,0);<br>isbool:=false;<br>这里循环?
 
悲伤的结束。
 
第二个问题,我再来讲讲吧,确实收不到!<br>HookHandle, dfhandle,你都定义的DLL的全局变量,但你这个DLL包含了消息钩子的回调函数,要注入到其他进程中去的,DLL的全局数据能够在不同进程中共享吗?原因就在这里。在其他进程中,dfhandle变量的值还是0,WM_COPYDATA消息哪里有发给主程序的主窗体?至于如何共享DLL的数据我就不多说了(通过内存映射文件来在不同进程之间共享DLL的全局数据即可)。
 
后退
顶部