COM method offsets in Delphi?

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.

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.

This looks promising... I'll try it later today and post the results. Thanks! – Dmitry Streblechenko Jul 2 '10 at 15:19.

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?

1 I am intercepting calls to an external COM object using Win32Hook. Pas - just a logging feature for my OutlookSpy (dimastr.com/outspy) – Dmitry Streblechenko Jul 1 '10 at 20:28 1 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 Jul 1 '10 at 21:13 Yeah, the compiler knows about it, but as far as I can tell, there's no way to get at it directly.

– Mason Wheeler Jul 2 '10 at 0:11.

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 = array0..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.

No, an interface variable in Delphi is a pointer to a v-table. – Dmitry Streblechenko Jul 4 '10 at 20:15 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 Jul 6 '10 at 3:22 @Rob Kennedy: thank you, the question forced me to write a blog post about Delphi interfaces: sergworks.wordpress.com/2010/07/06/… – Serg Jul 6 '10 at 8:05 Yes, sorry, you are exactly right. – Dmitry Streblechenko Jul 7 '10 at 23:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions