JCL中由接口获得对象的方法
2008-06-10 18:42
176 查看
我原本并不想单独发以下JCL的方法,可是想到很多人可能还是不知道的。另外,从我博客的回复上看,很多人也愿意去了解一下,特此贴出JCL中的代码。
//=== Interface information ================================================== function GetImplementorOfInterface(const I: IInterface): TObject; ...{ TODO -cDOC : Original code by Hallvard Vassbotn } ...{ TODO -cTesting : Check the implemetation for any further version of compiler } const AddByte = $04244483; // opcode for ADD DWORD PTR [ESP+4], Shortint AddLong = $04244481; // opcode for ADD DWORD PTR [ESP+4], Longint type PAdjustSelfThunk = ^TAdjustSelfThunk; TAdjustSelfThunk = packed record case AddInstruction: Longint of AddByte: (AdjustmentByte: ShortInt); AddLong: (AdjustmentLong: Longint); end; PInterfaceMT = ^TInterfaceMT; TInterfaceMT = packed record QueryInterfaceThunk: PAdjustSelfThunk; end; TInterfaceRef = ^PInterfaceMT; var QueryInterfaceThunk: PAdjustSelfThunk; begin try Result := Pointer(I); if Assigned(Result) then begin QueryInterfaceThunk := TInterfaceRef(I)^.QueryInterfaceThunk; case QueryInterfaceThunk.AddInstruction of AddByte: Inc(PChar(Result), QueryInterfaceThunk.AdjustmentByte); AddLong: Inc(PChar(Result), QueryInterfaceThunk.AdjustmentLong); else Result := nil; end; end; except Result := nil; end; end;注释:这段代码出自JCL代码中JclSysUtils单元中。提示:读这段代码,要明白一个道理,在代码执行过程中,接口和对象指针的偏移是硬编码的在汇编中的。转换的过程,就是解析这段汇编的过程。
相关文章推荐
- JCL中由接口获得对象的方法
- JCL中由接口获得对象的方法
- JCL中由接口获得对象的方法
- JCL中由接口获得对象的方法
- Java中获得一个Class对象的方法
- java从list中取出对象并获得其属性值的方法
- JSP内置对象:getattribute()方法获得数据
- 获取对象的接口信息(方法/属性/事件)(VB6代码)
- Java获得Class对象的方法
- Spring框架中获得DataSource对象的方法
- struts2中几种获得request,session等对象的方法
- Java 基础数据类型 和 深度克隆对象的2种方法(实现Cloneable接口或者实现对象序列化)
- golang里面检测对象是否实现了接口的方法
- ext中获得对象、组件的方法比较(Ext.getxxx())
- 常用方法(1)------根据图片的url路径获得Bitmap对象
- autofac如何注册静态方法里的接口对象
- JS获得一个对象的所有属性和方法
- 使用递归遍历对象获得value值的实现方法
- JavaScript获得指定对象大小的方法
- 黑马程序员----Set集合,Set派系的特点,Set接口方法,HashSet集合,对象的哈希值,TreeSet二叉树的排序效果