R
richardi
Unregistered / Unconfirmed
GUEST, unregistred user!
下面是我的代码,exe动态加载dll “baseinfo”,在dll的DLL_PROCESS_DETACH中写了卸载dll语句,实现dll封装的窗体关闭后就卸载dll,好节约内存,现在程序运行不报错,也没内存泄漏,就是当封装的MDI子窗体关闭后,dll不能自动卸载,还占据着内存,请教高手有什么办法能实现动态卸载dll,好节约内存。<br><br><br>library baseinfo;//dll的代码<br>uses<br> SysUtils,<br> Classes,<br> ADODB,<br> Windows,<br> Forms,<br> d_baseinfo in 'd_baseinfo.pas' {dfmBaseInfo};<br>var<br> DllApp:TApplication; //定义变量<br>{$R *.res}<br>procedure CallBaseinfo(const app:TApplication;const adocnn:Tadoconnection);stdcall;<br>begin<br> Application:=app;<br> if dfmBaseInfo=nil then<br> begin<br> dfmBaseInfo:=TdfmBaseInfo.Create(app);//创建mdi子窗体<br> dfmBaseInfo.ADOConnection1:=adocnn; //把参数adocnn对象赋给ADOConnection1获得数据库连接<br> end;<br> dfmBaseInfo.Show;<br>end;<br>procedure MyDLLProc(Reason:Integer);<br>begin<br> if Reason=DLL_PROCESS_DETACH then<br> begin<br> if Assigned(DllApp) then<br> Application:=DllApp;<br> FreeLibrary(Application.Handle); //进程退出时卸载dll<br> end;<br>end;<br>exports<br> CallBaseinfo;<br>begin<br> DllApp:=Application; //把dll的Application先存起来<br> DllProc:=@MyDLLProc; //<br>end. <br>//exe的调用代码<br>type<br> PlugIn=function(const app:TApplication;const adocnn:Tadoconnection):integer;stdcall;<br>……<br><br>procedure Tf_main.N3Click(Sender: TObject);<br>var<br> ahandle:THandle;<br> pluglugIn;<br>begin<br> ahandle:=LoadLibrary('baseinfo.dll') ; //动态加载DLL<br> if ahandle=0 then<br> exit;<br> @plug:=GetProcAddress(ahandle,'CallBaseinfo');<br> if @plug <>nil then<br> plug(Application,ADOConnection1);<br>end;