新手请教在dll封装了MDI子窗体,想从主窗体传递变量到dll的子窗体.

W9757 2009-10-30 02:55:04
这个例子是我从网上找的.方法是正确的.但现在有需要从主程序中传递DL_XM,DL_QX变量(string)到dll的子窗体中.源程序如下,请指教.........

DLL单元:
library MDIChild1;


uses
Windows,
Classes,
Controls,
Forms,
UnitMDIChildForm1 in 'UnitMDIChildForm1.pas' {MDIChildForm1};

{$R *.res}


function LoadForm(ParentApplication: TApplication; ParentForm: TForm): THandle; export; stdcall;
var
Form1: TMDIChildForm1;
DllProc: Pointer;
begin
Application:=ParentApplication;
Form1 := TMDIChildForm1.Create(ParentForm);
Form1.MyParentForm:=ParentForm;
Form1.MyParentApplication:=ParentApplication;
Form1.Show;
end;

procedure DLLUnloadProc(Reason: Integer); register;
begin
if Reason = DLL_PROCESS_DETACH then
Application:=DllApplication;
end;

exports
LoadForm;

begin
DllApplication := Application;
DLLProc := @DLLUnloadProc;
end.

DLL中mdichildform1单元:
unit UnitMDIChildForm1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;

type
TMDIChildForm1 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
MyParentForm: TForm;
MyParentApplication: TApplication;

end;

var
MDIChildForm1: TMDIChildForm1;
DllApplication: TApplication;

implementation

{$R *.dfm}

procedure TMDIChildForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action:=caFree;
end;

end.

主调程序单元:

unit UnitMainForm;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus;

type
TLoadForm = function(ParentApplication: TApplication; ParentForm: TForm): THandle; stdcall;

TMainForm = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
procedure N1Click(Sender: TObject);
private
{ Private declarations }
DLLModule: HModule;
LoadForm: TLoadForm;
procedure DLLLoad;
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.DLLLoad;
begin
DLLModule := LoadLibrary('MDIChild1.dll');
if DLLModule <> 0 then
try
@LoadForm := GetProcAddress(DLLModule, 'LoadForm');
except
ShowMessage ('Package not found');
end;
end;

procedure TMainForm.N1Click(Sender: TObject);
var
DllHandle: THandle;
ProcAddr: FarProc;
LoadForm: TLoadForm;
begin
DllHandle := LoadLibrary('MDIChild1');
ProcAddr := GetProcAddress(DllHandle, 'LoadForm');
if ProcAddr <> nil then
begin
LoadForm := ProcAddr;
LoadForm(Application,Self);
end;
end;

end.

...全文
148 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jxydhzw 2009-11-02
  • 打赏
  • 举报
回复
建议转成接口.别写方法或过程..
13193887977 2009-11-02
  • 打赏
  • 举报
回复
1楼的方法可以

另外顺便问一下:
我将exe和dll都使用带包编译,结果我发现exe在运行的时候图标没了,如果不带包,主程序的MDIChild不会包含dll的子窗体,怎么解决这个图标的问题?
W9757 2009-11-02
  • 打赏
  • 举报
回复
没有其它途径了吗?
W9757 2009-11-02
  • 打赏
  • 举报
回复
详细点可以吗?
W9757 2009-10-30
  • 打赏
  • 举报
回复
还有其它方法吗?
bdmh 2009-10-30
  • 打赏
  • 举报
回复
可以定义一个Record类型指针,用此指针传递,类似如下

type
PTest = ^TTest;
TTest = record
x: Integer;
y: Integer;
end;

主程序
procedure TForm1.Button1Click(Sender: TObject);
var
v1: PTest;
s:TSendRecord;
begin
New(v1);
FHandel := LoadLibrary('MyDll.dll');
if FHandel = 0 then
Exit;
@s := GetProcAddress(FHandel,'SendRecord');
if @s = nil then
Exit;
v1.x := 10;
v1.y := 15;
s(v1);
Dispose(v1);
FreeLibrary(FHandel);
end;

DLL
procedure SendRecord(rec:PTest);
begin
ShowMessage(IntToStr(rec.x + rec.y));
end;

5,517

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧