Last active
December 14, 2015 00:59
-
-
Save real-mielofon/5002732 to your computer and use it in GitHub Desktop.
Sample exception in RTTI-call safecall function method of interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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