tb0571.pp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. {$ifdef fpc}
  2. {$mode delphi}
  3. {$endif fpc}
  4. { Some (delphi) applications expect that the QueryInterface method is invoked as first
  5. priority to query for an interface and GetInterface as 2nd priority }
  6. uses
  7. sysutils;
  8. type
  9. ITest = interface
  10. ['{E80B0A2E-96ED-4F38-A6AC-E4E0B59F27F3}']
  11. end;
  12. TTest = class(TObject, IUnknown, ITest)
  13. private
  14. refcount: integer;
  15. public
  16. function QueryInterface(const iid : tguid;out obj) : Hresult;stdcall;
  17. function _AddRef : longint;stdcall;
  18. function _Release : longint;stdcall;
  19. end;
  20. var
  21. called: Boolean = False;
  22. function TTest.QueryInterface(const IID: TGUID; out Obj): Hresult; stdcall;
  23. begin
  24. called := true;
  25. if getinterface(iid,obj) then
  26. result:=S_OK
  27. else
  28. result:=longint(E_NOINTERFACE);
  29. end;
  30. function TTest._AddRef : longint;stdcall;
  31. begin
  32. Inc(refcount);
  33. result := refcount;
  34. end;
  35. function TTest._Release : longint;stdcall;
  36. begin
  37. Dec(refcount);
  38. result := refcount;
  39. end;
  40. var
  41. r: TTest;
  42. i: ITest;
  43. procedure get(out obj: ITest);
  44. begin
  45. obj := r as ITest;
  46. end;
  47. begin
  48. r := TTest.Create;
  49. r._AddRef;
  50. if not supports(r, ITest, i) or not called or (r.refcount<>2) then
  51. Halt(1);
  52. called := false;
  53. i := nil;
  54. get(i);
  55. if (i=nil) or not called or (r.refcount<>2) then
  56. Halt(1);
  57. i := nil;
  58. r._Release;
  59. end.