1

I came across this nice code from David Heffernan, but I cannot compile it in Lazarus.

Buffered files (for faster disk access)

I get 2 distinct error messages:

line 72 and 104: Duplicate identifier CacheSize --> I just renamed it and: CacheSize:=aCacheSize

line 53 and 78: No matching implementation for interface method QueryInterface ...

I have no idea how to mend that. I tried to create to new interface function simply calling the original function, but it doesn't work.

Help, please!

Community
  • 1
  • 1

2 Answers2

1

The first can be fixed by enabling delphi mode ({$mode delphi} after the interface line, -Sd on the commandline, or the relevant tick in Lazarus properties.

The second needs modifications. The "const" in QueryInterface must be changed to constref

{$ifdef fpc}
 function TBaseCachedFileStream.QueryInterface(constref IID: TGUID; out Obj): HResult;
{$else}
 function TBaseCachedFileStream.QueryInterface(const IID: TGUID; out Obj): HResult;
{$endif}

both in implementation and interface. This change was made because on Intel const usually implies by reference, and on other CPUs it doesn't and forcing all CONST to constref internally leads to slower code on those processors.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
0

Try this one In interface part:

protected
    function QueryInterface({$IFDEF FPC_HAS_CONSTREF}constref{$ELSE}const{$ENDIF} iid : tguid;out obj) : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
    function _AddRef : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
    function _Release : longint;{$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};

And in the implementation:

function TMyObject.QueryInterface(constref iid: tguid; out obj): longint;
begin
  if GetInterface(iid, obj) then
    Result := 0
  else
    Result := -1;
end;

function TMyObject._AddRef: longint;
begin
  Result := InterLockedIncrement(FRefCount);
end;

function TMyObject._Release: longint;
begin
  Result := InterLockedDecrement(FRefCount);
  if FRefCount = 0 then
    Free;
 end; 
Bert Verhees
  • 1,057
  • 3
  • 14
  • 25