dll中的窗体之间数据传递,该如何解决
dll中的窗体之间数据传递
外部程序调用一个dll函数创建一个窗体(此窗体为 a 窗体), 在此dll内部通过 a 窗体创建 b 窗体,并且把 a 窗体的数据传递给 b 窗体 进行操作。
-----------前面都能实现,接下来就不行了
b窗体操作完以后我要把数据传回给 a 窗体,结果内存报错!
请各位高手帮帮忙,先谢过了!
已经use unit1 ;
procedure TForm2.btn3Click(Sender:TObject);
begin
Form1.edt1.Text := '456 ';
end;
直接报错
------解决方案--------------------
建议在form2里面放一个public属性,返回值,
计算的时候
public
s: string;
procedure TForm2.btn3Click(Sender:TObject);
begin
s := '456 '; //放在返回值里;
end;
form1调用的时候:
if form2.ShowModal = mrOK then
begin
edt1.Text := s;
end;
------解决方案--------------------
我按你的想法试了一下, 没报错。下面是我的测试代码:(呵呵,我不是来灌水的)
Library Project1;//DLL
uses
Forms,
Unit1 in 'Unit1.pas ' {FrmA},
Unit2 in 'Unit2.pas ' {FrmB};
exports
crt,
crt2;
begin
end.
//------------------------
unit Unit1;// DLL窗体FrmA的单元
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFrmA = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure crt;
var
FrmA: TFrmA;
implementation
uses Unit2;
{$R *.dfm}
procedure Crt;
begin
FrmA := TFrmA.Create(nil);
FrmA.Edit1.Text := 'I ' 'm FrmA ';
FrmA.ShowModal;
FreeAndNil(FrmA);
end;
procedure TFrmA.Button1Click(Sender: TObject);
begin
Crt2;
end;
end.
//-----------------------
unit Unit2; //DLL窗体FrmB的单元
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFrmB = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure Crt2;
var
FrmB: TFrmB;
implementation
uses Unit1;
{$R *.dfm}
procedure Crt2;
begin
FrmB := TFrmB.Create(nil);
FrmB.Edit1.Text := 'FrmA ' 'Text is '+FrmA.Edit1.Text;
FrmB.ShowModal;
//FrmA.Edit1.Text := 'eeeeeee '; //这里也是可以的
FreeAndNil(FrmB);
end;
procedure TFrmB.Button1Click(Sender: TObject);
begin
FrmA.Edit1.Text := 'eeeeeee ';
end;
end.
//==============================
//调用
var
F : procedure;
H : THandle;
begin
H := LoadLibrary(PAnsiChar( 'Project1.dll '));
@F := GetProcAddress(H, 'crt ');
F;
FreeLibrary(H);
end;
外部程序调用一个dll函数创建一个窗体(此窗体为 a 窗体), 在此dll内部通过 a 窗体创建 b 窗体,并且把 a 窗体的数据传递给 b 窗体 进行操作。
-----------前面都能实现,接下来就不行了
b窗体操作完以后我要把数据传回给 a 窗体,结果内存报错!
请各位高手帮帮忙,先谢过了!
已经use unit1 ;
procedure TForm2.btn3Click(Sender:TObject);
begin
Form1.edt1.Text := '456 ';
end;
直接报错
------解决方案--------------------
建议在form2里面放一个public属性,返回值,
计算的时候
public
s: string;
procedure TForm2.btn3Click(Sender:TObject);
begin
s := '456 '; //放在返回值里;
end;
form1调用的时候:
if form2.ShowModal = mrOK then
begin
edt1.Text := s;
end;
------解决方案--------------------
我按你的想法试了一下, 没报错。下面是我的测试代码:(呵呵,我不是来灌水的)
Library Project1;//DLL
uses
Forms,
Unit1 in 'Unit1.pas ' {FrmA},
Unit2 in 'Unit2.pas ' {FrmB};
exports
crt,
crt2;
begin
end.
//------------------------
unit Unit1;// DLL窗体FrmA的单元
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFrmA = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure crt;
var
FrmA: TFrmA;
implementation
uses Unit2;
{$R *.dfm}
procedure Crt;
begin
FrmA := TFrmA.Create(nil);
FrmA.Edit1.Text := 'I ' 'm FrmA ';
FrmA.ShowModal;
FreeAndNil(FrmA);
end;
procedure TFrmA.Button1Click(Sender: TObject);
begin
Crt2;
end;
end.
//-----------------------
unit Unit2; //DLL窗体FrmB的单元
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TFrmB = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure Crt2;
var
FrmB: TFrmB;
implementation
uses Unit1;
{$R *.dfm}
procedure Crt2;
begin
FrmB := TFrmB.Create(nil);
FrmB.Edit1.Text := 'FrmA ' 'Text is '+FrmA.Edit1.Text;
FrmB.ShowModal;
//FrmA.Edit1.Text := 'eeeeeee '; //这里也是可以的
FreeAndNil(FrmB);
end;
procedure TFrmB.Button1Click(Sender: TObject);
begin
FrmA.Edit1.Text := 'eeeeeee ';
end;
end.
//==============================
//调用
var
F : procedure;
H : THandle;
begin
H := LoadLibrary(PAnsiChar( 'Project1.dll '));
@F := GetProcAddress(H, 'crt ');
F;
FreeLibrary(H);
end;