How I make RTTI-call with safecall function method of interface?
Asked Answered
L

1

14

I have this test program https://gist.github.com/real-mielofon/5002732

  RttiValue := RttiMethod.Invoke(RttiInstance, [10]);

and simple unit with interface:

unit Unit163;

interface

type
{$M+}
  ISafeIntf = interface
    function TestMethod(aI: integer): integer; safecall;
  end;
{$M-}
 type
   TSafeClass = class(TInterfacedObject, ISafeIntf)
   public
     function TestMethod(aI: integer): integer; safecall;
   end;

implementation

function TSafeClass.TestMethod(aI: integer): integer;
begin
  result := aI+1; // Exception !!
end;

end.

and I have kaboom on

result := aI+1;

if it is procedure or isn't safecall, then it's all right :-(

Loudish answered 21/2, 2013 at 6:40 Comment(0)
W
5

Having now tried this myself, and looked at the the code, my conclusion is that there is a bug. The RTTI unit does indeed attempt to perform safecall method re-writing. It just appears to get it wrong. I recommend that you submit your project as a QC report, and workaround the problem by using stdcall with HRESULT return values.

Woo answered 21/2, 2013 at 7:32 Comment(3)
I know about ":HRESULT; stdcall" and the only solution available today is delete safecall from source (and I doing it), but maybe there is a solution how to get to work with RTTI?Loudish
I don't think you need to change the declaration in the interface. Just pass two parameters when you call it via RTTI and read in the HRESULT as the return value of that call.Woo
If I change count parameters, then I have Exception in result: Parameter count mismatch. It is about: RttiHResultValue := RttiMethod.Invoke(RttiInstance, [10, RttiReturnValue]);Loudish

© 2022 - 2024 — McMap. All rights reserved.