Skip to content

Instantly share code, notes, and snippets.

@real-mielofon
Last active December 14, 2015 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save real-mielofon/5002732 to your computer and use it in GitHub Desktop.
Save real-mielofon/5002732 to your computer and use it in GitHub Desktop.
Sample exception in RTTI-call safecall function method of interface
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment