zywcd (2006-09-06 23:06:31) <br>今天测试通过的代码。<br>贴出来。<br>-------------------------------------------------<br>unit TestDLL;<br><br>interface<br><br>uses<br> SysUtils,Windows,Messages,Classes,Graphics,Controls,<br> Forms,Dialogs,StdCtrls,DB,ExtCtrls,DBTables;<br><br>type<br> TShowForm=function(mainform:Tform):Tform; StdCall;<br> tdllFormClose=procedure(tt:LongInt); StdCall;<br><br> EDLLLoadError=class(Exception);<br><br> TfrmCallDLL=class(Tform)<br> Database1:TDatabase;<br> btnCallDLL:TButton;<br> btnClose:TButton;<br> procedure btnCallDLLClick(Sender:TObject);<br> procedure btnCloseClick(Sender:TObject);<br> procedure FormClose(Sender:TObject; var Action:TCloseAction);<br> end;<br><br>var<br> frmCallDLL:TfrmCallDLL;<br><br>implementation<br><br>var<br> LibHandle:THandle;<br> ShowForm:TShowForm;<br> dllFormClose:tdllFormClose;<br> dllform:Tform;<br>{$R *.DFM}<br><br>procedure TfrmCallDLL.btnCallDLLClick(Sender:TObject);<br>begin<br> LibHandle:=LoadLibrary('RptDLL.DLL');<br> try<br> if LibHandle=HINSTANCE_ERROR then<br> raise EDLLLoadError.Create('Unable to Load DLL');<br> @ShowForm:=GetProcAddress(LibHandle,'ShowForm');<br> @dllFormClose:=GetProcAddress(LibHandle,'FormClose');<br> if not(@ShowForm=nil) then dllform:=ShowForm(frmCallDLL);<br> except<br> ;<br> end;<br>end;<br><br>procedure TfrmCallDLL.btnCloseClick(Sender:TObject);<br>begin<br>// Close;<br>end;<br><br>procedure TfrmCallDLL.FormClose(Sender:TObject; var Action:TCloseAction);<br>begin<br> dllform.Release;<br>//if LibHandle<>0 then FreeLibrary(LibHandle);<br>end;<br><br>end.<br><br><br>---------------------------------------<br><br>unit FormDLL;<br><br>interface<br><br>uses<br> SysUtils,Windows,Messages,Classes,Graphics,Controls,<br> Forms,Dialogs,StdCtrls, frxDesgn, frxClass; //,frxDBSet,frxClass;<br><br>type<br> TfrmDLL=class(TForm)<br> btnBioLifePrintPreview:TButton;<br> frxReport1: TfrxReport;<br> frxDesigner1: TfrxDesigner;<br> procedure btnBioLifePrintPreviewClick(Sender:TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>function ShowForm(mainform:TForm):TForm; StdCall;<br>procedure FormClose(AFormRef:LongInt); StdCall;<br><br>implementation<br><br>{$R *.DFM}<br><br>{------------------------------------------------------------------------}<br>function ShowForm(mainForm:TForm):TForm;stdcall<br>var<br> Form1: TfrmDLL;<br> ptr
LongInt;<br>begin<br> ptr:=@(Application.MainForm);<br> ptr^:=LongInt(mainForm);<br> Form1:=TfrmDLL.Create(mainForm);<br> Form1.Show;// .Show;<br>// Form1.<br> result:=Form1;<br><br>end;<br>procedure TfrmDLL.btnBioLifePrintPreviewClick(Sender:TObject);<br>begin<br> frxReport1.ShowReport;<br>end;<br><br>procedure FormClose(AFormRef:LongInt);<br>begin<br> if AFormRef>0 then<br> TfrmDLL(AFormRef).Release;<br>end;<br><br>end.