DLL的写法:
library pdll;
{ 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,
adodb,
db,
windows,
forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2};
{$R *.RES}
exports
run;
begin
END;
//DLL中FROM1的代码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, ADODB, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
q1: TADOQuery;
ds: TDataSource;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;
var form1:tform1;
function run(a:thandle;con:TADOConnection):boolean;stdcall;
implementation
uses Unit2;
{$R *.DFM}
function run(a:thandle;con:TADOConnection):boolean;stdcall;
begin
application.Handle:=a;
form1:=tform1.Create(application);
try
try
form1.q1.Connection:=con;
form1.showmodal;
result:=true;
except
result:=false;
end;
finally
form1.free;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
q1.open;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
application.createform(tform2,form2);
try
form2.showmodal;
finally
form2.free;
end;
end;
end.
end.
//DLL中FROM2的代码
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Buttons, Grids, DBGrids;
type
TForm2 = class(TForm)
BitBtn1: TBitBtn;
dg1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.DFM}
procedure TForm2.Button1Click(Sender: TObject);
begin
dg1.DataSource:=form1.dd;
caption:=inttostr(form1.q1.RecordCount)
end;
end.
主控程序:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, ADODB, Grids, DBGrids;
type
{ First, define a procedural data type, this should reflect the
procedure that is exported from the DLL. }
TRUN =function (a:thandle;con:TADOConnection):boolean;stdcall;
{ Create a new exception class to refect a failed DLL load }
EDLLLoadError = class(Exception);
TForm1 = class(TForm)
Button1: TButton;
adoc: TADOConnection;
q: TADOQuery;
ds: TDataSource;
DBGrid1: TDBGrid;
procedure FormShow(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormShow(Sender: TObject);
begin
q.open;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
LibHandle : THandle;
RUN: TRUN;
begin
{ Attempt to load the DLL }
LibHandle := LoadLibrary('PDLL.DLL');
try
{ If the load failed, LibHandle will be zero.
If this occurs, raise an exception. }
if LibHandle = 0 then
raise EDLLLoadError.Create('Unable to Load DLL');
{ If the code makes it here, the DLL loaded successfully, now obtain
the link to the DLL's exported function so that it can be called. }
@RUN := GetProcAddress(LibHandle, 'run');
{ If the function is imported successfully, then set lblDate.Caption to reflect
the returned date from the function. Otherwise, show the return raise
an exception. }
if not (@RUN = nil) then
RUN(Application.Handle, ADOC)
else
RaiseLastWin32Error;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
end;
end.