如何才能在dll窗口和主窗口之间传递参数呢?(40分)

  • 主题发起人 主题发起人 白晶晶
  • 开始时间 开始时间

白晶晶

Unregistered / Unconfirmed
GUEST, unregistred user!
例如:
当我点击主窗口的一个按钮时,便弹出dll窗口,dll窗口有两个edit框,我将数据输入到两个edit框并点击确定按钮后,主窗口的label1就显示两个edit框(dll的)中数据的和。
我该如何做呢?
谢谢了!
 
用值参返回
如DLL函数
AAA(a,b:string;var c:string);
a,b 两个edit的text
c:=a+b返回
 
DLL中最好不要用string, 用Pchar
 
可以在dll中定义一个函数
返回值为两个数的和
之后调用此函数
我试过了可以传值
 
用string
需要加ShareMem
否则可以用PChar
 
to chobits:
如何将数值传给主窗口上的控件呢,值是不确定的呀,给我源代码分都给你
 
DLL
Form没有关闭也许可以
如果已经关闭了呢?
肯定需要Var传值
 
sorry 我还是晕
能再详细点吗!
真的会感激你们的!!
 
我给你个代码吧,主要的功能是将dll窗体中的edit控件的值传给主窗体的edit控件
dll部分的代码
library DLLShowForm;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
ShareMem,
SysUtils,
Classes,
DLLFrm in 'DLLFrm.pas' {FrmDll};
{$R *.res}
exports
ShowDllModalForm,ShowDllform,test;
begin
end.
//注意啊,由于传的是string变量所以要在uses之后加上ShareMem
unit DLLFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, jpeg, ExtCtrls;
type
TFrmDll = class(TForm)
E1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure ShowDllModalForm(aHandle:THandle);stdcall;
procedure ShowDllForm(aHandle:THandle);stdcall;
function test():string;stdcall;
var
FrmDll: TFrmDll;
sig:string;
implementation
{$R *.dfm}
procedure ShowDllModalForm(aHandle:THandle);
begin
Application.Handle:=aHandle;
with TfrmDll.Create(application)do
show;
end;
procedure ShowDllForm(aHandle:THandle);
begin
Application.Handle:=aHandle;
with TfrmDll.Create(application)do
show;
end;
function test():string;
begin
test:=sig;
end;
procedure TFrmDll.Button1Click(Sender: TObject);
begin
sig:=E1.Text;
end;

end.
//dll窗体的源代码

//主窗体的源代码
unit MainFrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TTest=function ():string;stdcall;
EDLLError=class(Exception);
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
E1: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure ShowDllModalForm(aHandle:THandle);
stdcall external '../1/DllShowForm.dll';
procedure ShowDllForm(aHandle:THandle);
stdcall external '../1/DllShowForm.dll';
function test():string;
stdcall external '../1/DllShowForm.dll';
var
Form1: TForm1;
DllHandle:THandle;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowDllModalForm(application.Handle);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ShowDllform(Application.Handle);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
DLLHandle:=LoadLibrary('../1/DLLshowForm.dll');
if DLlHandle=0 then
raise EDllError.Create('找不到dll文件');
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
FreeLibrary(DLLHandle);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
t:TTest;
begin
@t:=GetProcAddress(DLLHandle,'test');
if (@t=nil) then
RaiseLastWin32Error;
e1.Text:=test();
end;

end.

//只演示了,传值的功能,在dll窗体中的edit控件中输入一个值,点按钮后,可以
//关闭窗体也可不关,在主窗体中点按钮,值就可以传过来了
 
恐怕要用ycxy的方法
DLL中有FORM,必须为SHOWMODAL
释放后就没有了
 
让你们久等了!不好意思我忘记结贴了:)
 
后退
顶部