首先将Form2编进DLLPro.dll中,怎么编@##¥¥%^&J*,代码如下:////////////////////////DLLPro.dpr///////////////////////////////// library DLLPro;{ 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 SysUtils, Classes, Unit2 in 'Unit2.pas' {Form2};{$R *.res}exports myShowForm2, myCloseForm2;begin end.///////////////////////Unit2.pas/////////////////////////////////// unit Unit2;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type TForm2 = class(TForm) private { Private declarations } public { Public declarations } end;{var Form2: TForm2;}function myShowForm2(AHandle:THandle):longint;stdcall; procedure myCloseForm2(AFormRef:longint);stdcall;implementation{$R *.dfm}function myShowForm2(Ahandle:Thandle):longint; var form2:Tform2; begin application.Handle:=AHandle; form2:=tform2.Create(application); form2.Show; result:=longint(form2); end;procedure myCloseForm2(AformRef:longint); begin if AFormRef>0then Tform2(AformRef).Release; end;end. 编绎成功后,在任何工程中都可以调用DllPro.dll,显示非模式的Form2,如下: ////////////////////////Project1.dpr///////////////////////////// program Project1;uses Forms, Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin Application.Initialize; Application.CreateForm(TForm1, Form1); Application.Run; end.///////////////////////////Unit1.pas/////////////////////////////// unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end;var Form1: TForm1; FormRef:longint;function myShowForm2(AHandle:THandle):longint;stdcall; procedure myCloseForm2(AFormRef:longint);stdcall;implementationfunction myShowForm2;external 'DLLPro.dll'; procedure myCloseForm2;external 'DllPro.dll';{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin FormRef:=myShowForm2(application.Handle); end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin myCloseForm2(FormRef); end;end. ------------------------------------------------------------------- 风过西窗客渡舟船无觅处 年年一川新草遥看却似旧
如果你了解一点COM,我想这个问题很简单。
DLL文件: library Main;uses Main in 'Main.pas' {fmMain};exports ShowForm;{$R *.res}begin end.PAS文件: unit Main;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, hbDialog, DB, DBClient, MConnect, StdCtrls, Buttons, ActiveX; type ... var ... procedure ShowForm(可以加些参数); stdcall;implementationprocedure ShowForm(); begin try ShowModal; finally Free; end; end; 调用DLL中的函数:procedure TForm2.Button1Click(Sender: TObject); var aHandle: THandle; ShowForm: procedure(); stdcall; begin aHandle := LoadLibrary(Main.DLL'); if aHandle <> 0 then @ShowForm := GetProcAddress(aHandle,'ShowForm'); if @ShowForm <> nil then begin try ShowForm(); finally FreeLibrary(aHandle); end; end; end;
library DLLPro;{ 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
SysUtils,
Classes,
Unit2 in 'Unit2.pas' {Form2};{$R *.res}exports
myShowForm2,
myCloseForm2;begin
end.///////////////////////Unit2.pas///////////////////////////////////
unit Unit2;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm2 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
end;{var
Form2: TForm2;}function myShowForm2(AHandle:THandle):longint;stdcall;
procedure myCloseForm2(AFormRef:longint);stdcall;implementation{$R *.dfm}function myShowForm2(Ahandle:Thandle):longint;
var
form2:Tform2;
begin
application.Handle:=AHandle;
form2:=tform2.Create(application);
form2.Show;
result:=longint(form2);
end;procedure myCloseForm2(AformRef:longint);
begin
if AFormRef>0then
Tform2(AformRef).Release;
end;end.
编绎成功后,在任何工程中都可以调用DllPro.dll,显示非模式的Form2,如下:
////////////////////////Project1.dpr/////////////////////////////
program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.///////////////////////////Unit1.pas///////////////////////////////
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
FormRef:longint;function myShowForm2(AHandle:THandle):longint;stdcall;
procedure myCloseForm2(AFormRef:longint);stdcall;implementationfunction myShowForm2;external 'DLLPro.dll';
procedure myCloseForm2;external 'DllPro.dll';{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
FormRef:=myShowForm2(application.Handle);
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
myCloseForm2(FormRef);
end;end.
-------------------------------------------------------------------
风过西窗客渡舟船无觅处
年年一川新草遥看却似旧
library Main;uses
Main in 'Main.pas' {fmMain};exports
ShowForm;{$R *.res}begin
end.PAS文件:
unit Main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, hbDialog, DB, DBClient, MConnect, StdCtrls, Buttons, ActiveX;
type
...
var
...
procedure ShowForm(可以加些参数); stdcall;implementationprocedure ShowForm();
begin
try
ShowModal;
finally
Free;
end;
end;
调用DLL中的函数:procedure TForm2.Button1Click(Sender: TObject);
var
aHandle: THandle;
ShowForm: procedure(); stdcall;
begin
aHandle := LoadLibrary(Main.DLL');
if aHandle <> 0 then
@ShowForm := GetProcAddress(aHandle,'ShowForm');
if @ShowForm <> nil then
begin
try
ShowForm();
finally
FreeLibrary(aHandle);
end;
end;
end;