使用dll进行对象的封装:
使用Dll来封装对象。 主要限制在于: 。调用dll的应用程序只能使用dll中对象的动态绑定的方法。这点是必须的。 。dll封装的对象只能在dll中进行创建。 。在dll和调用dll的应用程序中都需要对封装的对象及其被调用的方法进行声明。 实现dll中对象的动态绑定的方法可以使用virtual和接口两种方法来进行。 使用抽象类也可以是实现dll中对象的动态绑定。
1.简单demo演示dll进行类对象的封装。
a.dll
unit untobj;
interface
type
TMan = class// 类的实现
private
FAge: Integer;
public
function GetAge: Integer; virtual; //使用后期绑定的方法。
constructor Create;
end;
implementation
{ TMan }
constructor TMan.Create;
begin
FAge := 13;
end;
function TMan.GetAge: Integer;
begin
Result := FAge;
end;
end.
uses
SysUtils,
Classes,
untobj in 'untobj.pas';
// 主要限制在于:
//。调用dll的应用程序只能使用dll中对象的动态绑定的方法。这点是必须的。
//。dll封装的对象只能在dll中进行创建。
//。在dll和调用dll的应用程序中都需要对封装的对象及其被调用的方法进行声明。
{$R *.res}
function ManObj: TMan; //在dll中到处类对象。
begin
Result := TMan.Create;
end;
exports
manobj;
begin
end.
2.调用dll的实现的过程。
unit untDemo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMan = class //在调用dll的程序中进行相应类的生明。
public
function GetAge: Integer; virtual;abstract; // 使用抽象的方法来进行声明。
end;
type
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function ManObj: TMan; external 'dlldemo.dll'; //引用dll中的函数。
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
var
man: TMan;
i: Integer;
begin
man := ManObj; // 调用程序中使用dll导出的对象。
if man = nil then Exit;
try
i := man.GetAge;
ShowMessage(IntToStr(i));
finally
man.Free;
end;
end;
end.
第二:使用接口来实现类的封装的简单示例:
1.dll中类以及接口的声明和实现。
unit untISay;// 接口的声明。
interface
type
ISay = interface
['{BB0B17F4-92FB-4B10-83BF-89300D58B8BA}']
procedure say;
function sayInteger: Integer;
end;
implementation
end.
--------------------------------------------------------------------------------------------
// 类的实现。
unit untMan;
interface
uses Dialogs,untISay;
type
TMan = class(TInterfacedObject,ISay)
procedure Say;
function SayInteger:Integer;
end;
implementation
{ TMan }
procedure TMan.Say;
begin
ShowMessage('nihaoma');
end;
function TMan.SayInteger: Integer;
begin
Result := 100;
end;
end.
--------------------------------------------------------------------------
dll中导出相应的接口的函数。
uses
SysUtils,
Classes,
untISay in 'untISay.pas',
untMan in 'untMan.pas';
{$R *.res}
function outputobj: ISay;
begin
Result := TMan.create;// 这个关键的地方。
end;
exports
outputobj;
begin
end.
在调用dll程序中的
unit untmain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,untISay, StdCtrls;// 主要添加相应的UntISay接口声明的单元
type
TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
m: ISay;//
end;
var
Form1: TForm1;
function outputobj:ISay; external 'dllobj.dll'; //----------------------------
implementation
{$R *.dfm}
procedure TForm1.btn1Click(Sender: TObject);
begin
m := outputobj;
m.say;
m := nil;
end;
procedure TForm1.btn2Click(Sender: TObject);
begin
m := outputobj;
ShowMessage(IntToStr(m.sayInteger));
m := nil;
end;
end.
第三。使用抽象类的情况。