public
Last active

Sample exception in RTTI-call safecall function method of interface

  • Download Gist
gistfile1.pas
Pascal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
program Project94;
 
uses
Winapi.Windows, System.SysUtils, System.Classes, System.Rtti,
System.Generics.Collections,
Unit163 in 'Unit163.pas';
 
procedure Main;
var
SafeIntf: ISafeIntf;
RttiContext: TRttiContext;
RttiType: TRttiType;
RttiMethod: TRttiMethod;
RttiValue: TValue;
RttiInstance: TValue;
begin
SafeIntf := TSafeClass.Create;
try
RttiContext.Create;
try
RttiType := RttiContext.FindType('Unit163.ISafeIntf');
RttiInstance := TValue.From<IInterface>(SafeIntf);
if RttiType <> nil then begin
try
for RttiMethod in RttiType.GetMethods do begin
if SameText(RttiMethod.Name, 'TestMethod') then begin
RttiValue := RttiMethod.Invoke(RttiInstance, [10]);
Break;
end;
end;
finally
RttiType.Destroy;
end;
end;
finally
RttiContext.Free;
end;
finally
SafeIntf := nil;
end;
end;
 
begin
Main;
end.
 
// Unit
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.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.