Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COM method offsets in Delphi

Tags:

com

delphi

vtable

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!

like image 428
Dmitry Streblechenko Avatar asked Feb 04 '23 04:02

Dmitry Streblechenko


1 Answers

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:

call dword ptr [eax] + vmtoffset IInterface.QueryInterface
...
call dword ptr [eax] + vmtoffset IInterface._Release

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.

like image 59
Rob Kennedy Avatar answered Feb 13 '23 23:02

Rob Kennedy