简体   繁体   English

在哪里可以找到有关Delphi VMT结构的信息?

[英]Where can I find information on the structure of the Delphi VMT?

The System.pas file contains a fair amount of information on hard-coded VMT offsets, but it doesn't seem to actually say much about the structure of the VMT itself. System.pas文件包含有关硬编码VMT偏移量的大量信息,但实际上似乎并未真正说明VMT本身的结构。 What I'd really like to know is, is there any way to find out the size of a VMT at runtime, or in other words, how many virtual methods exist for a given class? 我真正想知道的是,有没有办法在运行时找出VMT的大小,或者换句话说,给定类有多少虚拟方法?

What about the VMT structure are you wanting to know? 你想知道VMT结构怎么样? You also do know that it is an internal implementation detail that is subject to change (and has changed over time). 您还知道它是一个内部实现细节,可能会发生变化(并且随着时间的推移而发生变化)。

To answer your specific question, here is a simple way to find the number of virtual methods for a given class: 要回答您的具体问题,以下是查找给定类的虚拟方法数的简单方法:

function GetVirtualMethodCount(AClass: TClass): Integer;
begin
  Result := (PInteger(Integer(AClass) + vmtClassName)^ - 
    (Integer(AClass) + vmtParent) - SizeOf(Pointer)) div SizeOf(Pointer);
end;

This works because I happen to know that the string representing the class name is placed immediately following all the virtual method vectors in the VMT. 这是有效的,因为我碰巧知道表示类名的字符串紧跟在VMT中的所有虚拟方法向量之后。

I also know that there are 11 virtual methods (for D2009, 9 for D2007 and prior) on all TObjects that are negatively offset from the VMT pointer itself. 我还知道在所有TObject上有11个虚拟方法(对于D2009,对于D2007和之前的9个),它们与VMT指针本身有负向偏移。

That is the reason for the vmtParent reference. 这就是vmtParent引用的原因。

Finally, by using a TClass class reference, you can pass any TObject derived class into this function and get the number of virtual methods. 最后,通过使用TClass类引用,您可以将任何TObject派生类传递给此函数并获取虚拟方法的数量。

I was pretty sure Hallvard had something on the VMT. 我很确定Hallvard在VMT上有一些东西。 Sure enough, he has Hack #8: Explicit VMT calls which references Ray Lischner Secrets of Delphi 2 and Delphi in a Nutshell . 果然,他有Hack#8:显式VMT调用 ,它引用了Delphi 2中的 Ray Lischner SecretsDelphi中的Delphi

Here is his hacked up version of the VMT 这是他的黑客攻击版本的VMT

type
  PClass = ^TClass;
  PSafeCallException = function  (Self: TObject; ExceptObject:
    TObject; ExceptAddr: Pointer): HResult;
  PAfterConstruction = procedure (Self: TObject);
  PBeforeDestruction = procedure (Self: TObject);
  PDispatch          = procedure (Self: TObject; var Message);
  PDefaultHandler    = procedure (Self: TObject; var Message);
  PNewInstance       = function  (Self: TClass) : TObject;
  PFreeInstance      = procedure (Self: TObject);
  PDestroy           = procedure (Self: TObject; OuterMost: ShortInt);
  PVmt = ^TVmt;
  TVmt = packed record
    SelfPtr           : TClass;
    IntfTable         : Pointer;
    AutoTable         : Pointer;
    InitTable         : Pointer;
    TypeInfo          : Pointer;
    FieldTable        : Pointer;
    MethodTable       : Pointer;
    DynamicTable      : Pointer;
    ClassName         : PShortString;
    InstanceSize      : PLongint;
    Parent            : PClass;
    SafeCallException : PSafeCallException;
    AfterConstruction : PAfterConstruction;
    BeforeDestruction : PBeforeDestruction;
    Dispatch          : PDispatch;
    DefaultHandler    : PDefaultHandler;
    NewInstance       : PNewInstance;
    FreeInstance      : PFreeInstance;
    Destroy           : PDestroy;
   {UserDefinedVirtuals: array[0..999] of procedure;}
  end;

You will need to read his article for more on the hack though. 你需要阅读他的文章,了解有关黑客的更多信息。

Googling :-P for "delphi vmt" yields this . 谷歌搜索:-P为“德尔福vmt”产生这个 Maybe this gives you a start. 也许这会给你一个开始。

I'll plug my own site for this one: 我将插入我自己的网站:

What is the virtual-method table? 什么是虚方法表?

It's accurate as of Delphi 2005, I think. 我认为,从Delphi 2005开始,它是准确的。

The VMT does not have any value giving the number of virtual-method pointers it holds. VMT没有任何给出它所拥有的虚方法指针数的值。 Nothing but the compiler needs to know that information, so there's no reason to record it for use at run time. 除了编译器需要知道该信息之外什么都没有,因此没有理由将其记录下来以便在运行时使用。

i remember there was some information about delphi vmt in "delphi in a nutshell" book. 我记得有一些关于delphi vmt的信息,请参阅“delphi简介”一书。 u can start from delphi in a nutshell chapter 2 or this ü可以从开始德尔福简而言之第2章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM