DLL的入出口问题,(初始化问题)(50分)

  • 主题发起人 主题发起人 nero_p
  • 开始时间 开始时间
N

nero_p

Unregistered / Unconfirmed
GUEST, unregistred user!
正常情况下,在调用DLL时,可以使用DLLENTRYPOINT来初始化进程和线程时<br>比如调用DLL时会传递DLL_PROCESS_ATTACH参数,而结束调用DLL时或者调用freelibrary()<br>时会传递DLL_PROCESS_DETACH,而创建一个线程时,会传递DLL_THREAD_ATTACH,通例还有DLL_THREAD_DETACH<br>但是我参考书上的例子,却只会传递DLL_PROCESS_ATTACH,释放时或创建线程时都没有反映,并不<br>触发相应事件?<br>为什么?<br>另外:DLLEntryPoint(DLL_PROCESS_ATTACH);什么意思?<br><br>DLL文件:<br>library DLLEntryLib;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Windows,<br>&nbsp; Dialogs,<br>&nbsp; Classes;<br><br>procedure DLLEntryPoint(dwReason: DWord);<br>begin<br>&nbsp; case dwReason of<br>&nbsp; &nbsp; DLL_PROCESS_ATTACH: ShowMessage('Attaching to process');<br>&nbsp; &nbsp; DLL_PROCESS_DETACH: ShowMessage('Detaching from process');<br>&nbsp; &nbsp; DLL_THREAD_ATTACH: &nbsp;MessageBeep(0);<br>&nbsp; &nbsp; DLL_THREAD_DETACH: &nbsp;MessageBeep(0);<br>&nbsp; end;<br>end;<br><br>begin<br>&nbsp; DllProc :=@DLLEntryPoint;<br>&nbsp; DLLEntryPoint(DLL_PROCESS_ATTACH);<br>end.<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; //定义一个TTHREAD的继承类<br>&nbsp; TTestthread=class(TTHread)<br>&nbsp; &nbsp; procedure execute;override;<br>&nbsp; &nbsp; procedure setcaptiondata;<br>&nbsp; end;<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; Button4: TButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; libhandle:thandle;<br>&nbsp; &nbsp; testthread:ttestthread;<br>&nbsp; &nbsp; counter:integer;<br>&nbsp; &nbsp; gothread:boolean;<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>procedure &nbsp;TTestthread.execute;<br>begin<br>&nbsp;while form1.gothread do<br>&nbsp; begin<br>&nbsp; &nbsp;synchronize(setcaptiondata);//在主线程中执行setcaptiondata;<br>&nbsp; &nbsp;inc(form1.counter);<br>&nbsp; end;<br>end;<br>procedure TTestthread.setcaptiondata;<br>begin<br>&nbsp;form1.Label1.Caption:=inttostr(form1.counter);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>if libhandle=0 then<br>&nbsp;begin<br>&nbsp; libhandle:=loadlibrary('dllentrylib.dll');<br>&nbsp; if libhandle=0 then<br>&nbsp; &nbsp;raise exception.Create('不能加载dllentrylib.dll');<br>&nbsp;end<br>else<br>&nbsp;messagedlg('dllentrylib.dll已经被加载了',mtwarning,[mbok],0);<br>end;<br><br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>if not (libhandle=0) then<br>&nbsp;begin<br>&nbsp; freelibrary(libhandle);<br>&nbsp; libhandle:=0;<br>&nbsp;end;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br>if testthread=nil then<br>&nbsp;begin<br>&nbsp; gothread:=true;<br>&nbsp; testthread:=ttestthread.Create(false);<br>&nbsp;end;<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br>if not(testthread=nil) then<br>&nbsp;begin<br>&nbsp; gothread:=false;<br>&nbsp; testthread.Free;<br>&nbsp; testthread:=nil;<br>&nbsp; counter:=0;<br>&nbsp;end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>libhandle:=0;<br>testthread:=nil;<br>end;<br><br>end.
 
兄弟们,怎么没人帮忙啊!<br>我都困惑了好几天了!
 
没有搞错,还是没有人!上厕所不带纸的那些家伙们都干什么去了--搞手<br>哎!郁闷啊!
 
你装了SP2没有?<br>我以前也是这样的,装SP2后就好了。好像是DELPHI的BUG。
 
其实如果你只是想在DLL刚被调用和释放时执行一些动作(如初始化和释放资源),也不必使用这么复杂的东东![:D]<br>我通常的做法是使用init...(拼写忘了)和fini...段。前者在DLL初次调用时执行,后者在不再调用时(通常是程序结束运行时)执行。<br>你可能会问:多个程序调用了该DLL怎么办?回答是:没问题,每个程序调用时都得重新初始化!
 
to:begindelphi<br>我用的D6,另外那里有sp2<br>to:解元<br>能说的具体些吗!
 
DLL的初始化当然是,每次调用都初始化了。<br>sp2,去搜一下吧,更新了不少东西
 
begin<br>end.<br>之间的部分就是dll的初始化部分,而我现在想知道的是dll的释放部分,ExitProc好像<br>并不执行。<br><br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1219913
 
呆呵。UPUP
 
后退
顶部