Delphi 中的 COM 方法偏移量
在Delphi中,如何找到COM方法的地址? 我可以对偏移量进行硬编码
//0 is the offset of the QueryInterface method
p := TPonterArray(pointer(SomeInterface)^)[0];
,但我更喜欢使用符号名称。以下显然不起作用:
var M : TMethod;
...
M := TMethod(SomeInterface.QueryInterface);
谢谢!
In Delphi, how do I find out the the address of a COM method?
I can hardcode the offsets
//0 is the offset of the QueryInterface method
p := TPonterArray(pointer(SomeInterface)^)[0];
but I would prefer to use symbolic names. The folllowing obviously does not work:
var M : TMethod;
...
M := TMethod(SomeInterface.QueryInterface);
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
vmtoffset
汇编器指令来获取接口方法相对于接口方法表开头的字节偏移量。看一下System.pas中_IntfCast
的实现,例如:第一个表达式加0;第二个是 8。
不过,您不能参数化这些表达式。它们是编译时常量,因此您无法在运行时选择所需的方法。您需要提前表示所有可能的方法名称。
您真正需要挂钩的是
QueryInterface
。一旦你有了它,你就可以返回任何你想要的代理对象,它可以拦截对所有其他方法的调用。You can use the
vmtoffset
assembler directive to get the byte offset of an interface method relative to the start of the interface's method table. Take a look at the implementation of_IntfCast
in System.pas, for example:The first expression adds 0; the second, 8.
You cannot parameterize those expressions, though. They're compile-time constants, so you cannot choose which method you want at run time. You need to have all possible method names represented in advance.
All you really need to hook is
QueryInterface
. Once you have that, you can return whatever proxy object you want that can intercept calls to all the other methods.我认为德尔福不支持这一点。对偏移量进行硬编码可能是唯一可行的方法,因为编译器不会将接口方法视为其值可以分配给函数指针的符号,就像对象方法或独立函数那样。
顺便说一句,你为什么要这样做?
I don't think Delphi supports that. Hardcoding the offsets is probably the only thing that will work, since the compiler doesn't count interface methods as symbols whose value can be assigned to a function pointer, the way object methods or standalone functions can.
Why are you trying to do this, BTW?
您的代码是错误的,因为接口引用不是指向接口方法表的指针,而是指向接口方法表的指针。这就是 Delphi 接口在二进制级别上的实现方式。很难说更多并指出代码中的错误,因为您没有给出可以编译的代码示例。使用以下代码将接口引用正确转换为方法指针,该想法取自 Barry Kelly 从方法引用创建方法指针的演示:
如果您更喜欢 MethNo 的符号名称,您最好自己将它们声明为偏移常量
Your code is wrong because an interface reference is not a pointer to an interface method table but a pointer to pointer to an interface method table. That is how Delphi interfaces are implemented on binary level. It is hard to say more and point out to the error in your code because you have not given a code example that can be compiled. Use the following code to convert interface reference to method pointer correctly, the idea was taken from Barry Kelly's demonstration of creating a method pointer from a method reference:
If you prefer symbolic names for MethNo you are better to declare them yourself as offset constants
docwiki.embarcadero.com
这里是获取所需方法指针的代码
docwiki.embarcadero.com
Here the code to get the needed method pointer