我给你个代码吧,主要的功能是将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控件中输入一个值,点按钮后,可以
//关闭窗体也可不关,在主窗体中点按钮,值就可以传过来了