unit uMyCOM;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, ActiveX, Classes, ComObj;

type
  TMyCOM = class(TObject, IUnknown)
  private
    FRefCount: Integer;
  protected
    { IUnknown }
    function IUnknown.QueryInterface = ObjQueryInterface;
    function IUnknown._AddRef = ObjAddRef;
    function IUnknown._Release = ObjRelease;
  public
    function ObjAddRef: Integer; virtual; stdcall;
    function ObjQueryInterface(const IID: TGUID; out Obj): HResult; virtual; stdcall;
    function ObjRelease: Integer; virtual; stdcall;
  end;

const
  Class_MyCOM: TGUID = '{2D80D5A3-01FC-4E30-B7DB-74F7301CE7C9}';

implementation

function TMyCOM.ObjAddRef: Integer;
begin
  Result := InterlockedIncrement(FRefCount);
end;

function TMyCOM.ObjRelease: Integer;
begin
  Result := InterlockedDecrement(FRefCount);
  if Result = 0 then
    Destroy;
end;

function TMyCOM.ObjQueryInterface(const IID: TGUID; out Obj): HResult;
begin
  if GetInterface(IID, Obj) then Result := S_OK else Result := E_NOINTERFACE;
end;

end.
