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;