D
David1289
Unregistered / Unconfirmed
GUEST, unregistred user!
一个Dll中的两个Form可以相互调用吗? Form1随Dll的调用而显示,后调用Form2,在将结果返回Form1, Form1调用Form2可以,Form2调用form1出错(Access violition ...at address ...),但再加一form3, Form2与form3之间调用完全正常,不知为何,请高手赐教。谢谢。<br><br>//===================Exe程序<br><br><br>unit UntMain;<br><br>interface<br><br>uses <br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button3: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button3Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>TCommRoute = Procedure(D_Account: string); stdcall;<br><br>var<br> Form1: TForm1;<br> D_Account, dll_filename: pchar; // dll_path,<br> DllHandle: THandle;<br> CommRoute: TCommRoute;<br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> DllHandle := LoadLibrary(PChar('Proj1Dll.dll'));<br> if DllHandle <= 0 then<br> application.messagebox('系统应用程序不存在!', '')<br> else<br> begin<br> @CommRoute := GetProcAddress(DllHandle, 'ProcTest');<br> if not Assigned(@CommRoute) then<br> application.messagebox('系统应用程序出错', '')<br> else<br> CommRoute(D_Account);<br> end;<br> FreeLibrary(DllHandle);<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br> Close ;<br>end;<br><br>end.<br>//==================Dll<br>library Proj1Dll;<br><br>uses<br> SysUtils,<br> Classes,<br> Unit1 in 'Unit1.pas' {Form1},<br> Unit2 in 'Unit2.pas' {Form2},<br> Unit3 in 'Unit3.pas' {Form3};<br><br>{$R *.res}<br><br>begin<br><br>end.<br>//======================unit1<br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br> Function ProcTest(D_Account: Pchar): Integer; stdcall;<br> exports<br> ProcTest;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Edit1: TEdit;<br> procedure Button2Click(Sender: TObject);<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form1: TForm1;<br><br>implementation<br><br>uses Unit2;<br><br>{$R *.dfm}<br>Function ProcTest(D_Account: pchar):Integer;<br>begin<br> With TForm1.Create(Form1) do<br> try<br> // AdoConnection1 := Adoconn;<br> // AdoQuery1.ConnectionString := Adoconnection1.ConnectionString;<br> // Position := poScreenCenter;<br> ShowModal;<br> finally<br> free;<br> end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> close ;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> if not Assigned(Form2) then<br> Form2 := TForm2.Create(self) ;<br> Form2.ShowModal ;<br>end;<br><br>end.<br>//=====================unit2<br>unit Unit2;<br><br>interface<br><br>uses <br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm2 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Edit1: TEdit;<br> Button3: TButton;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure Button3Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form2: TForm2;<br><br>implementation<br> uses unit1, unit3 ;<br>{$R *.dfm}<br><br><br>procedure TForm2.Button1Click(Sender: TObject);<br>begin<br> // if not Assigned(Form1) then<br> // Form1 := TForm1.Create(self) ; //=========把这两行注释掉会显示access<br>// violition...at address ..错误 <br> // Form1.ShowModal ; //不注释掉则新创建窗体<br> unit1.Form1.Edit1.Text :=Edit1.Text ;<br> Close ;<br><br>end;<br><br>procedure TForm2.Button2Click(Sender: TObject);<br>begin<br> close ;<br>end;<br><br>procedure TForm2.Button3Click(Sender: TObject);<br>begin<br> if not Assigned(Form3) then<br> Form3 := TForm3.Create(self) ;<br> Form3.ShowModal ;<br>end;<br><br>end.<br>//=====================unit3<br>unit Unit3;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls;<br><br>type<br> TForm3 = class(TForm)<br> Button1: TButton;<br> Edit1: TEdit;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Form3: TForm3;<br><br>implementation<br> uses unit2 ;<br>{$R *.dfm}<br><br>procedure TForm3.Button1Click(Sender: TObject);<br>begin<br> unit2.Form2.Edit1.Text := Edit1.Text ;<br> close ;<br>end;<br><br>end.<br><br>