Delphi接口示例代码
发布时间:2020-12-15 09:55:27 所属栏目:大数据 来源:网络整理
导读: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; funct
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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |