procedure 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;
{ 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;
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;