给自己一点时间接受自己,爱自己,趁着下午茶的时间来学习图老师推荐的Com接口入门细详(二),过去的都会过去,迎接崭新的开始,释放更美好的自己。
【 tulaoshi.com - 编程语言 】
Com接口入门细详(二)
{$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;
来源:http://www.tulaoshi.com/n/20160219/1606608.html
看过《Com接口入门细详(二)》的人还看了以下文章 更多>>