Delphi

本类阅读TOP10

·分布式网络考试系统原型分析及实现
·游戏外挂设计技术探讨①
·使用HOOK随心监视Windows
·Delphi 水晶报表打包解决
·试题库开发中非文本数据的处理
·如何将几个DBGRID里的内容导入同一个EXCEL表中....的问题
·如何使用Delphi设计强大的服务器程序
·工人线程中关闭窗体的实现
·用DLL方式封装MDI子窗体。
·支持XP下托盘栏气球提示的托盘单元

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Com接口入门细详(二)

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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;




相关文章

相关软件