tw15363.pp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. {$mode delphi}
  2. uses
  3. Classes, SysUtils;
  4. type
  5. ITest = interface ['{AAAA09DA-4019-4A5C-A450-3631A73CF288}']
  6. function TestIt: integer;
  7. end;
  8. TTestBE = class (TObject, ITest)
  9. function TestIt: integer;
  10. { IInterface }
  11. function _AddRef: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  12. function _Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  13. function QueryInterface(constref IID: TGUID; out Obj): HResult; virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  14. End;
  15. TTest = class (TPersistent, IInterface)
  16. BE : TTestBE;
  17. protected
  18. { IInterface }
  19. function _AddRef: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  20. function _Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  21. function QueryInterface(constref IID: TGUID; out Obj): HResult; virtual; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
  22. End;
  23. function TTestBE.TestIt : integer;
  24. Begin
  25. result := 1;
  26. End;
  27. function TTest._AddRef: Integer;
  28. begin
  29. Result := -1;
  30. end;
  31. function TTest._Release: Integer;
  32. begin
  33. Result := -1;
  34. end;
  35. function TTest.QueryInterface(constref IID: TGUID; out Obj): HResult;
  36. begin
  37. Result := BE.QueryInterface(IID, obj);
  38. end;
  39. function TTestBE._AddRef: Integer;
  40. begin
  41. Result := -1;
  42. end;
  43. function TTestBE._Release: Integer;
  44. begin
  45. Result := -1;
  46. end;
  47. function TTestBE.QueryInterface(constref IID: TGUID; out Obj): HResult;
  48. begin
  49. if GetInterface(IID, Obj)
  50. then Result := 0
  51. end;
  52. Var
  53. Test : TTest;
  54. A : ITest;
  55. begin
  56. Test := TTest.Create;
  57. Test.BE := TTestBE.Create;
  58. // Works ok in Lazarus and Delphi
  59. Test.BE.GetInterface (ITest, A);
  60. // Works ok in Lazarus. Delphi will not compile this line
  61. A := Test.BE As ITest;
  62. // Both Delphi and Lazarus return nil ptr
  63. Test.GetInterface(ITest, A);
  64. // Works in Lazarus
  65. Test.QueryInterface (ITest, A);
  66. // Lazarus throws typecast error.
  67. // Works fine in delphi because delphi calls QueryInterface while Lazarus does not
  68. A := Test As ITest;
  69. end.