tags:

views:

187

answers:

3

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!

+2  A: 

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?

Mason Wheeler
I am intercepting calls to an external COM object using Win32Hook.pas - just a logging feature for my OutlookSpy (http://www.dimastr.com/outspy/)
Dmitry Streblechenko
BTW, Delphi compiler obviously knows the method offsets, I am just trying to find a way to do the same instead of using hardcoded offsets.
Dmitry Streblechenko
Yeah, the compiler knows about it, but as far as I can tell, there's no way to get at it directly.
Mason Wheeler
+4  A: 

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.

Rob Kennedy
This looks promising... I'll try it later today and post the results.Thanks!
Dmitry Streblechenko
+1  A: 

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:

procedure IntRefToMethPtr(const IntRef; var MethPtr; MethNo: Integer);
type
  TVtable = array[0..999] of Pointer;
  PVtable = ^TVtable;
  PPVtable = ^PVtable;
begin
  // QI=0, AddRef=1, Release=2, etc
  TMethod(MethPtr).Code := PPVtable(IntRef)^^[MethNo];
  TMethod(MethPtr).Data := Pointer(IntRef);
end;

If you prefer symbolic names for MethNo you are better to declare them yourself as offset constants

Serg
No, an interface variable in Delphi is a pointer to a v-table.
Dmitry Streblechenko
No, Dmitry, Serg is right. An interface variable cannot just be a pointer to a vtable. All instances of the class share a single vtable, just like non-interface classes. Consider C++, in which there is no difference between interfaces and ordinary classes. An object pointer is not just a vtable pointer, so neither is an interface pointer.
Rob Kennedy
@Rob Kennedy: thank you, the question forced me to write a blog post about Delphi interfaces:http://sergworks.wordpress.com/2010/07/06/delphi-interfaces-on-binary-level/
Serg
Yes, sorry, you are exactly right.
Dmitry Streblechenko