Com接口入门细详(二)

王朝delphi·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Com接口入门细详(二)

上一篇,简单的详细了com的应用,不过那不是com接口发挥作用所在,要不这种东东早就淘汰了,com的接口真正作用其实就是提供实现对象给客户程序利用,而com又可分为进程内com(dll文件),进程外com(exe文件).

现在让我们来了解一下进程内com接口的应用。

Com即然提供其中的类方法给客户程序,那么把将要生成的dll文件,com对象表示为服务端

(为人民服务嘛,呵呵)

当然建com服务器

新建activeX library

在其中新建com对象,这基本应该大家会的吧。

主要还是分析代码

unit Unit1;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses

Windows, ActiveX, Classes, ComObj;

type

ICalculator= interface

['{214C8A93-C235-45DB-BEDB-460DA54F3B01}']

function Add(x,y:Integer):Integer;safecall;

function Mult(x,y:Integer):Integer;safecall;

end;

TCalculator = class(TComObject, ICalculator)

protected

{Declare ICalculator methods here}

function Add(x,y:Integer):Integer;safecall; // 加法运算

function Mult(x,y:Integer):Integer;safecall; // 乘法运算

end;

const

Class_Calculator: TGUID = '{E81D22BE-7203-4447-B65C-6FF4CFA7E982}';//声明guid值这是唯一的。

implementation

uses ComServ;

function TCalculator.Add(x, y: Integer): Integer;

begin

Result:= x+y ;//

end;

function TCalculator.Mult(x, y: Integer): Integer;

begin

Result:= x*y ;

end;

initialization

TComObjectFactory.Create(ComServer, TCalculator, Class_Calculator,

'Calculator', '', ciMultiInstance, tmApartment);//初始化建立唯一的guid值的com对象

end.

―――――――

客户端当然来利用这个dll文件。

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls,ActiveX;

const

Class_Calculator: TGUID='{E81D22BE-7203-4447-B65C-6FF4CFA7E982}';//跟服务端值一样下面我们注册对象,依这个值向注册表寻找guid值

type

ICalculator= interface

['{214C8A93-C235-45DB-BEDB-460DA54F3B01}']

function Add(x,y:Integer):Integer;safecall;

function Mult(x,y:Integer):Integer;safecall;

end;

TForm1 = class(TForm)

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Label4: TLabel;

Edit1: TEdit;

Edit2: TEdit;

Edit3: TEdit;

Edit4: TEdit;

Button1: TButton;

procedure Button1Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

FCal:ICalculator;

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

uses ComServ;

procedure TForm1.Button1Click(Sender: TObject);

begin

Edit3.Text:= IntToStr(FCal.Add(StrToInt(Edit1.Text), StrToInt(Edit2.Text)));

Edit4.Text:= IntToStr(FCal.Mult(StrToInt(Edit1.Text), StrToInt(Edit2.Text)));

end;

procedure TForm1.FormCreate(Sender: TObject);

begin

CoCreateInstance(Class_Calculator,nil,CLSCTX_INPROC_SERVER,ICalculator,FCal);//建立com对象实例

end;

end.

――――――――――――――――――――――――――――――――――――

搞定当然我们要向系统注册这个com对象文件,要不客户如何应用,

当然注册这个dll依照这个guid值的来的,这是唯一的,

注册com对象文件

Regsvr32 dll文件路径

――――――――――――――――

对于上一篇的代码还不够简单,让大家再简单的com接口应用

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

ipggpjj= Interface(IUnknown)

procedure pggpjj1;

nd;

Tpggpjj=class(TInterfacedObject,Ipggpjj)

public

procedure pggpjj1;

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

procedure Tpggpjj.pggpjj1;

begin

showmessage('成功');

end;

procedure TForm1.Button1Click(Sender: TObject);

var pggpjj:ipggpjj;

begin

pggpjj:=Tpggpjj.Create;

pggpjj.pggpjj1;

end;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航