在哪里可以找到有关 Delphi VMT 结构的信息? [英] Where can I find information on the structure of the Delphi VMT?

查看:35
本文介绍了在哪里可以找到有关 Delphi VMT 结构的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.pas 文件包含大量关于硬编码 VMT 偏移量的信息,但它似乎并没有真正说明 VMT 本身的结构.我真正想知道的是,有没有办法在运行时找出 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. 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? 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;

这是可行的,因为我碰巧知道表示类名的字符串紧跟在 VMT 中的所有虚拟方法向量之后.

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.

我还知道所有 TObject 上有 11 个虚方法(对于 D2009,9 个对于 D2007 和之前),它们与 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.

这就是 vmtParent 引用的原因.

That is the reason for the vmtParent reference.

最后,通过使用 TClass 类引用,您可以将任何 TObject 派生类传递给该函数并获取虚拟方法的数量.

Finally, by using a TClass class reference, you can pass any TObject derived class into this function and get the number of virtual methods.

这篇关于在哪里可以找到有关 Delphi VMT 结构的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆