DLL中两个Form可以相互调用吗?谢谢 (30分)

  • 主题发起人 主题发起人 David1289
  • 开始时间 开始时间
D

David1289

Unregistered / Unconfirmed
GUEST, unregistred user!
&nbsp;一个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 &nbsp;<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>TCommRoute = Procedure(D_Account: string); stdcall;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; D_Account, dll_filename: pchar; // dll_path,<br>&nbsp; DllHandle: THandle;<br>&nbsp; CommRoute: TCommRoute;<br>implementation<br><br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; DllHandle := LoadLibrary(PChar('Proj1Dll.dll'));<br>&nbsp; if DllHandle &lt;= 0 then<br>&nbsp; &nbsp; application.messagebox('系统应用程序不存在!', '')<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; @CommRoute := GetProcAddress(DllHandle, 'ProcTest');<br>&nbsp; &nbsp; &nbsp; if not Assigned(@CommRoute) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; application.messagebox('系统应用程序出错', '')<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CommRoute(D_Account);<br>&nbsp; &nbsp; end;<br>&nbsp; FreeLibrary(DllHandle);<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; Close ;<br>end;<br><br>end.<br>//==================Dll<br>library Proj1Dll;<br><br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; Unit1 in 'Unit1.pas' {Form1},<br>&nbsp; Unit2 in 'Unit2.pas' {Form2},<br>&nbsp; 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>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>&nbsp; Function ProcTest(D_Account: Pchar): Integer; stdcall;<br>&nbsp; exports<br>&nbsp; ProcTest;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>uses Unit2;<br><br>{$R *.dfm}<br>Function ProcTest(D_Account: pchar):Integer;<br>begin<br>&nbsp; With TForm1.Create(Form1) do<br>&nbsp; try<br>&nbsp; &nbsp;// AdoConnection1 := Adoconn;<br>&nbsp; &nbsp;// AdoQuery1.ConnectionString := Adoconnection1.ConnectionString;<br>&nbsp; &nbsp;// Position := poScreenCenter;<br>&nbsp; &nbsp; ShowModal;<br>&nbsp; finally<br>&nbsp; &nbsp; free;<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; close ;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; if not Assigned(Form2) then<br>&nbsp; &nbsp; Form2 := TForm2.Create(self) ;<br>&nbsp; &nbsp; Form2.ShowModal ;<br>end;<br><br>end.<br>//=====================unit2<br>unit Unit2;<br><br>interface<br><br>uses <br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm2 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form2: TForm2;<br><br>implementation<br>&nbsp; &nbsp;uses unit1, unit3 ;<br>{$R *.dfm}<br><br><br>procedure TForm2.Button1Click(Sender: TObject);<br>begin<br>&nbsp;// if not Assigned(Form1) then<br>&nbsp; // &nbsp;Form1 := TForm1.Create(self) ; &nbsp; //=========把这两行注释掉会显示access<br>// violition...at address ..错误 <br>&nbsp;// Form1.ShowModal ; //不注释掉则新创建窗体<br>&nbsp; unit1.Form1.Edit1.Text &nbsp;:=Edit1.Text &nbsp;;<br>&nbsp; Close ;<br><br>end;<br><br>procedure TForm2.Button2Click(Sender: TObject);<br>begin<br>&nbsp; close ;<br>end;<br><br>procedure TForm2.Button3Click(Sender: TObject);<br>begin<br>&nbsp; if not Assigned(Form3) then<br>&nbsp; &nbsp; Form3 := TForm3.Create(self) ;<br>&nbsp; Form3.ShowModal ;<br>end;<br><br>end.<br>//=====================unit3<br>unit Unit3;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm3 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form3: TForm3;<br><br>implementation<br>&nbsp; uses unit2 ;<br>{$R *.dfm}<br><br>procedure TForm3.Button1Click(Sender: TObject);<br>begin<br>&nbsp; unit2.Form2.Edit1.Text := Edit1.Text ;<br>&nbsp; close ;<br>end;<br><br>end.<br><br>
 
你在Form1的Button1Click将Form2给ShowModal了,在Form2中又怎么能再对Form1进行ShowModal呢,就算不在dll中,你也做不到啊。
 
to TYZhang:<br>&nbsp; 在Form2中不用showmodal ,直接引用uses unit1来引用form1中的控件,调用返回时有错误,access violation ..at address....,编译时也通不过。
 
因为你的Form1创建有问题,这样改试试:<br>Function ProcTest(D_Account: pchar):Integer;<br>begin<br>&nbsp; if not Assigned(Form1) then Form1:=TForm1.Create(nil)<br>&nbsp; With Form1 do<br>&nbsp; //With TForm1.Create(Form1) do &nbsp;//你原来的代码创建Form1时,没有对Form1变量进行赋值,在Form2中引用当然会报错。<br>&nbsp; try<br>&nbsp; &nbsp;// AdoConnection1 := Adoconn;<br>&nbsp; &nbsp;// AdoQuery1.ConnectionString := Adoconnection1.ConnectionString;<br>&nbsp; &nbsp;// Position := poScreenCenter;<br>&nbsp; &nbsp; ShowModal;<br>&nbsp; finally<br>&nbsp; &nbsp; free;<br>&nbsp; end;<br>&nbsp; Form1:=nil;<br>end;<br>
 
form之間的互相調用有時會出問題, 建議設主一個公共form來引用...
 
你的建的第一窗口是主窗口,在主窗口下的其他窗口可以,但是对与主窗口的话,必须变化指定主窗口!
 
试试在第一个form的 interface uses 加 unit1<br>&nbsp; &nbsp; &nbsp; 第二个form的 implementation uses 加 unit2<br>
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
777
import
I
I
回复
0
查看
701
import
I
S
回复
0
查看
844
SUNSTONE的Delphi笔记
S
I
回复
0
查看
690
import
I
I
回复
0
查看
791
import
I
后退
顶部