JCL中由接口获得对象的方法

我原本并不想单独发以下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单元中。

提示:读这段代码,要明白一个道理,在代码执行过程中,接口和对象指针的偏移是硬编码的在汇编中的。转换的过程,就是解析这段汇编的过程。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值