天天看點

JCL中由接口獲得對象的方法

  1. //=== Interface information ==================================================  
  2. function GetImplementorOfInterface(const I: IInterface): TObject;  
  3.   { TODO -cDOC : Original code by Hallvard Vassbotn }  
  4.   { TODO -cTesting : Check the implemetation for any further version of compiler }  
  5.  const  
  6.    AddByte = $04244483; // opcode for ADD DWORD PTR [ESP+4], Shortint  
  7.    AddLong = $04244481; // opcode for ADD DWORD PTR [ESP+4], Longint  
  8.  type  
  9.    PAdjustSelfThunk = ^TAdjustSelfThunk;  
  10.    TAdjustSelfThunk = packed record  
  11.      case AddInstruction: Longint of 
  12.        AddByte: (AdjustmentByte: ShortInt);  
  13.        AddLong: (AdjustmentLong: Longint);  
  14.    end;  
  15.    PInterfaceMT = ^TInterfaceMT;  
  16.    TInterfaceMT = packed record  
  17.      QueryInterfaceThunk: PAdjustSelfThunk;  
  18.    end;  
  19.    TInterfaceRef = ^PInterfaceMT;  
  20.  var  
  21.    QueryInterfaceThunk: PAdjustSelfThunk;  
  22.  begin 
  23.    try  
  24.      Result := Pointer(I);  
  25.      if Assigned(Result) then 
  26.      begin 
  27.        QueryInterfaceThunk := TInterfaceRef(I)^.QueryInterfaceThunk;  
  28.        case QueryInterfaceThunk.AddInstruction of 
  29.          AddByte:  
  30.            Inc(PChar(Result), QueryInterfaceThunk.AdjustmentByte);  
  31.          AddLong:  
  32.            Inc(PChar(Result), QueryInterfaceThunk.AdjustmentLong);  
  33.        else 
  34.          Result := nil;  
  35.        end;  
  36.      end;  
  37.    except 
  38.      Result := nil;  
  39.    end;  
  40.  end; 

繼續閱讀