to:人在昆明<br><br>为什么我按你的例子进行测试时在主程序关闭后就会出现AV错误?<br><br>to LiChaoHui:<br> 给我一个txyqbf_Gxx@163.com<br>其实没什么可以保留的,我就贴一个我写得吧(SDI):<br>exe 调用dll 中窗体的例子,sdi 和 mdi 基本上一样:<br>DLL 部分:<br><br>{****************************************************************}<br>{ }<br>{ Project: UDllTest }<br>{ Copyright(c) 2003, 2005 }<br>{ Unit for UDllTest }<br>{ Create : 2003-01-05 by 林红卫 }<br>{ }<br>{****************************************************************}<br><br>library UDllTest;<br><br>uses<br> SysUtils,<br> Forms,<br> Messages,<br> Windows,<br> Classes,<br> UFrmTestForm1 in 'UFrmTestForm1.pas' {Form1},<br> UFrmTestForm2 in 'UFrmTestForm2.pas' {Form2};<br><br>var<br> DLLApp: TApplication;<br> DLLScreen: TScreen;<br> TheForm: TForm;<br> theClass: TPersistentClass;<br><br>procedure RunDLL(DLLName, FormName, FormCaption: string;<br> APP: TApplication; SC: TScreen) stdcall;<br><br>begin<br> Application := App;<br> Screen := sc;<br> RegisterClasses([TForm1, TForm2]);<br><br> theClass := GetClass('T' + FormName);<br><br> if theClass.InheritsFrom(TForm) and (theClass <> nil) then<br> begin<br> TheForm := TForm(theClass.Create).Create(Application);<br> TheForm.Caption := FormCaption;<br> TheForm.Show;<br> end;<br>end;<br><br>procedure DLLUnloadProc(Reason: Integer);<br>begin<br> if Reason = DLL_PROCESS_DETACH then<br> begin<br> Application := DLLApp; //恢复<br> Screen := DLLScreen;<br> FreeAndNil(TheForm);<br> FreeAndNil(theClass); // 卸载时释放资源<br> end;<br>end;<br><br>exports<br> RunDLL;<br><br>begin<br> DLLApp := Application; //保存 DLL 中初始的 Application 对象<br> DLLScreen := Screen;<br> DLLProc := @DLLUnloadProc; //保证 DLL 卸载时恢复原来的 Application<br>end.<br>这是dpr 你加入form1,form2 就行啦!<br><br>exe 的dll 调用部分:<br><br>{****************************************************************}<br>{ }<br>{ Project: DllDebug }<br>{ Copyright(c) 2003, 2005 }<br>{ Unit for UCommonUnit }<br>{ Create : 2003-01-05 by 林红卫 }<br>{ }<br>{****************************************************************}<br><br>unit UCommonUnit;<br><br>interface<br><br>uses<br> Windows,<br> SysUtils,<br> Forms;<br><br>type<br> TRunDLL = procedure(DLLName, FormName, FormCaption: string;<br> Application: TApplication; Screen: TScreen) stdcall;<br><br>procedure RunDLLForm(DLLName, FormName, FormCaption: string;<br> APP: TApplication; SC: TScreen) stdcall;<br><br>implementation<br><br>procedure RunDLLForm(DLLName, FormName, FormCaption: string;<br> APP: TApplication; SC: TScreen) stdcall;<br>var<br> GetDllHWND: HWND;<br> RunDLL: TRunDLL;<br>begin<br> GetDllHWND := LoadLibrary(PChar(DllName));<br> if GetDllHWND < 32 then<br> begin<br> MessageBox(0, '没有找到附带DLL文件,请确认程序是否完整!',<br> '加载DLL失败', MB_OK);<br> Exit;<br> end;<br><br> @RunDLL := GetProcAddress(GetDllHWND, 'RunDLL');<br> if @RunDLL <> nil then<br> begin<br> try<br> RunDLL(UpperCase(Trim(DLLName)), UpperCase(Trim(FormName)),<br> FormCaption, APP, SC);<br> except<br> raise Exception.Create('T' + FormName + '不存在');<br> end;<br> end;<br>end;<br>end.<br>dll 的Form Name 可以从Exe 传入。<br>