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;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐