• Delphi接口示例代码


      
      IMyInterface = interface(IInterface)
        ['{63E072DF-B81E-4734-B3CB-3C23C7FDA8EA}']
        function F1 : Integer; stdcall;
      end;
    
      TFooBar = class(TBaseProperty, IMyInterface)
        function F1 : Integer; virtual; stdcall;
      protected
        FRefCount: Integer;
        function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
        function _AddRef: Integer; stdcall;
        function _Release: Integer; stdcall;
      end;
    
      TFooBar1 = class(TFooBar)
        function F1: Integer;  override; stdcall;
      end;
    

      

    procedure TForm1.Button2Click(Sender: TObject);
    var
      a: TFooBar;
      dd: IMyInterface;
    begin
      a := TFooBar1.Create;
    
      if a.GetInterface(IMyInterface, dd) then
        Memo1.Lines.Add(IntToStr(dd.F1));
    
    end;
    

      

    function TFooBar.QueryInterface(const IID: TGUID; out Obj): HResult;
    const
      E_NOINTERFACE = HResult($80004002);
    begin
      if GetInterface(IID, Obj) then
        Result := 0
      else
        Result := E_NOINTERFACE;
    end;
    
    function TFooBar._AddRef: Integer;
    begin
      INC(FRefCount);
    //  ShowMessage(Format('Increase reference count to %d.', [FRefCount]));
      result:=FRefCount;
    end;
    
    function TFooBar._Release: Integer;
    begin
     DEC(FRefCount);
      if FRefCount <> 0 then
    //    ShowMessage(Format('Decrease reference count to %d.', [FRefCount]))
      else begin
        Destroy;
    //    ShowMessage('Decrease reference count to 0, and destroy the object.');
      end;
      result:=FRefCount;
    end;
    

      

  • 相关阅读:
    查找算法:二分查找法(折半查找)
    钞票找零-贪心,动态规划算法
    PHP7与php5
    网站高并发解决方案(理论知识) 二
    loj#6566. 月之都的密码
    我的 Linux 配置
    CTSC2011 幸福路径
    WC2018 即时战略
    uoj#460 新年的拯救计划
    bzoj 5016 一个简单的询问
  • 原文地址:https://www.cnblogs.com/tsolarboy/p/9442645.html
Copyright © 2020-2023  润新知