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> SysUtils,<br> Windows,<br> Dialogs,<br> Classes;<br><br>procedure DLLEntryPoint(dwReason: DWord);<br>begin<br> case dwReason of<br> DLL_PROCESS_ATTACH: ShowMessage('Attaching to process');<br> DLL_PROCESS_DETACH: ShowMessage('Detaching from process');<br> DLL_THREAD_ATTACH: MessageBeep(0);<br> DLL_THREAD_DETACH: MessageBeep(0);<br> end;<br>end;<br><br>begin<br> DllProc :=@DLLEntryPoint;<br> DLLEntryPoint(DLL_PROCESS_ATTACH);<br>end.<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> //定义一个TTHREAD的继承类<br> TTestthread=class(TTHread)<br> procedure execute;override;<br> procedure setcaptiondata;<br> end;<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Button3: TButton;<br> Button4: TButton;<br> Label1: TLabel;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure Button3Click(Sender: TObject);<br> procedure Button4Click(Sender: TObject);<br> procedure FormCreate(Sender: TObject);<br> private<br> libhandle:thandle;<br> testthread:ttestthread;<br> counter:integer;<br> gothread:boolean;<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br>procedure TTestthread.execute;<br>begin<br> while form1.gothread do<br> begin<br> synchronize(setcaptiondata);//在主线程中执行setcaptiondata;<br> inc(form1.counter);<br> end;<br>end;<br>procedure TTestthread.setcaptiondata;<br>begin<br> form1.Label1.Caption:=inttostr(form1.counter);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>if libhandle=0 then<br> begin<br> libhandle:=loadlibrary('dllentrylib.dll');<br> if libhandle=0 then<br> raise exception.Create('不能加载dllentrylib.dll');<br> end<br>else<br> 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> begin<br> freelibrary(libhandle);<br> libhandle:=0;<br> end;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br>if testthread=nil then<br> begin<br> gothread:=true;<br> testthread:=ttestthread.Create(false);<br> end;<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br>if not(testthread=nil) then<br> begin<br> gothread:=false;<br> testthread.Free;<br> testthread:=nil;<br> counter:=0;<br> end;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>begin<br>libhandle:=0;<br>testthread:=nil;<br>end;<br><br>end.