请问如何把一个接口用dll封装? ( 积分: 100 )

  • 主题发起人 主题发起人 dejoy
  • 开始时间 开始时间
D

dejoy

Unregistered / Unconfirmed
GUEST, unregistred user!
请问如何把一个接口用dll封装?
我想把某个功能做成接口,并用dll封装,在使用时只要调用dll中的公布的接口就行了,请问该怎么做?
 
请问如何把一个接口用dll封装?
我想把某个功能做成接口,并用dll封装,在使用时只要调用dll中的公布的接口就行了,请问该怎么做?
 
OCX不就是DLL吗??
 
和OCX应该还是有区别的吧?
 
http://www.2ccc.com/article.asp?articleid=1618
给你个网址看看
 
先用 new 新建个dll 就像 新建个 unit
然后
library test;

uses
SysUtils,
Classes,
test in test.pas';

{$R *.res}

function test;boolean;StdCall;
begin
end;
{引出函数名}
exports
test;
end.
编译 生成 test.dll

使用的时候 定义一个Unit
unit test;

interface
{$ifndef pennieslib}
{由关键词 Export 声明函数}
function test:boolean;StdCall;
{$endif}
implementation
{$ifndef pennieslib}
{定义引入的函数}
function test;external test.dll' name test;
{$endif}

end.
 
Unit publicDLL;

Interface

Uses SysUtils, Variants, Classes, Windows, Forms;

Type
TbaseformClass = Class Of TForm1;
Procedure getDate(Var DateWeek: widestring);
Type
Tpub = Class
public
Procedure getDate(Var DateWeek: widestring); virtual; abstract;
End;


Function Createpub: Tpub; stdcall; external 'public.dll';
Var
pub: Tpub;

Implementation

Procedure getDate(Var DateWeek: widestring);
Begin
pub.getDate(DateWeek);
End;

Initialization
pub := Createpub;
Finalization
pub.Free;

End.
 
interface
type
IPlugin = interface
['{D3F4445A-C704-42BC-8283-822541668919}'] // ? Ctrl+Shift+G ==> GUID
function CreateForm(hMainForm: THandle): THandle;
procedure DestroyForm;
function ShowModalForm: Integer;
end;

TPlugin = class(TInterfacedObject, IPlugin)
private
FForm: TForm;
public
destructor Destroy; override;

function CreateForm(hMainForm: THandle): THandle;
procedure DestroyForm;
function ShowModalForm: Integer;
end;

function CreatePlugin(hApp: THandle): IPlugin; export; stdcall;

exports
CreatePlugin;

implementation

function CreatePlugin(hApp: THandle): IPlugin;
begin
if hApp <> 0 then Application.Handle := hApp; // Sync Application handle.
Result := TPlugin.Create;
end;
 
//下例來子劉藝的《Delphi面向對象編程思想》

unit Demo;
interface
uses
SysUtils, Windows, Messages, Classes, Graphics, Controls,
Forms, Dialogs,IDemo;
type
TEngine = class(TObject)
private
FCapacity: Integer;
FPower: Integer;
public
constructor create;
procedure start;
procedure stop;
end;
TWheel = class(TObject)
private
FNo: Integer;
FSize: Integer;
FTypeName: string;
procedure check;
public
constructor create(size:Integer;TypeName:string;No:Integer);
end;
TVehicle = class(TInterfacedObject)
protected
FColor: string;
FMake: string;
FTopSpeed: Integer;
FWheel: TWheel;
FWheels: TList;
procedure SlowDown;
procedure SpeedUp;
procedure Start;
procedure Stop;
end;
TBicycle = class(TVehicle,IBicycle)
public
constructor create;
destructor Destory;
procedure ride;
end;
TCar = class(TVehicle,ICar)
protected
FEngine: TEngine;
public
constructor create;
destructor Destory;
procedure drive;
end;
implementation
{
*********************************** TBicycle ***********************************
}
constructor TBicycle.create;
var
i: Integer;
begin
FColor:='白色';
FMake:='永久';
FTopSpeed:=20;
ShowMessage(FColor+FMake+'車,最高時速:'+IntToStr(FTopSpeed));
FWheels:= TList.Create;
for i:=1 to 2 do
FWheels.Add(TWheel.create(21,'B型自行車車輪',i));
end;
destructor TBicycle.Destory;
var
i: Integer;
begin
for i:=1 to 2 do
TWheel(FWheels.Items).Free;
inherited;
end;
procedure TBicycle.ride;
begin
start;
speedUp;
showmessage('自行車車行駛中...');
SlowDown;
Stop;
end;
{
************************************* TCar *************************************
}
constructor TCar.create;
var
i: Integer;
begin
inherited create;
FEngine:=TEngine.create;
FColor:='黑色';
FMake:='紅旗';
FTopSpeed:=200;
ShowMessage(FColor+FMake+'車,最高時速:'+IntToStr(FTopSpeed));
FWheels:= TList.Create;
for i:=1 to 4 do
FWheels.Add(TWheel.create(36,'A型汽車車輪',i));
end;
destructor TCar.Destory;
var
i: Integer;
begin
for i:=1 to 4 do
TWheel(FWheels.Items).Free;
FEngine.Free;
inherited;
end;
procedure TCar.drive;
begin
start;
FEngine.start;
speedUp;
showmessage('汽車行駛中...');
SlowDown;
FEngine.stop;
Stop;
end;
{
*********************************** TEngine ************************************
}
constructor TEngine.create;
begin
Fcapacity:=1500;
Fpower:=130;
end;
procedure TEngine.start;
begin
ShowMessage(inttostr(Fcapacity)+'cc,'+inttostr(Fpower)+'匹馬力的發動機發動了!');
end;
procedure TEngine.stop;
begin
ShowMessage('發動機關閉了');
end;
{
*********************************** TVehicle ***********************************
}
procedure TVehicle.SlowDown;
begin
ShowMessage('正在減速...');
end;
procedure TVehicle.SpeedUp;
begin
ShowMessage('正在加速...');
end;
procedure TVehicle.Start;
begin
ShowMessage('車子開始啟動');
end;
procedure TVehicle.Stop;
begin
ShowMessage('車子停下');
end;
{
************************************ TWheel ************************************
}
constructor TWheel.create(size:Integer;TypeName:string;No:Integer);
begin
FSize:=size;
FTypeName:=TypeName;
FNo:=No;
check;
end;
procedure TWheel.check;
begin
ShowMessage('檢查第'+IntToStr(FNo)+'個車輪。型號:'+FTypeName+' 大小:'+IntToStr(FSize));
end;
end.

//=================================================================================
//Dll 文件,這里要封裝的接口
library DemoSvr;
{ 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,
Demo in 'Demo.PAS',
IDemo in 'IDemo.PAS';
{$R *.res}
function CarObj:ICar;
begin
Result:=TCar.create;
end;

function BicycleObj:IBicycle;
begin
Result:=TBicycle.create;
end;

exports
CarObj,BicycleObj;
end.

//=================================================================================

unit frmDemo;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IDemo;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
function CarObj:ICar ;external 'DemoSvr.dll';
function BicycleObj:IBicycle ;external 'DemoSvr.dll';

implementation
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
var MyCar:ICar;
begin
MyCar:=CarObj;
MyCar.drive;
Mycar:=nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Bicycle:IBicycle;
begin
Bicycle:=BicycleObj;
Bicycle.ride;
Bicycle:=nil;
end;
end.
//=================================================================================
unit IDemo;
interface
type
ICar = interface (IInterface)
['{ED52E264-6683-11D7-B847-001060806215}']
procedure Drive;
end;
IBicycle = interface (IInterface)
['{ED52E264-6683-11D7-B847-001060806216}']
procedure Ride;
end;
implementation
end.

//=================================================================================
program ObjDemo;
uses
Forms,
frmDemo in 'frmDemo.PAS' {Form1},
IDemo in 'IDemo.PAS';
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
 
值得好好学习
 
后退
顶部