[英]How can I get the address of a COM object's member function?
I have a pointer to a COM object pfoo implements IFoo. 我有一个指向COM对象的指针,pfoo实现了IFoo。 I'd like to get the address of pfoo->Bar(), one of the methods of IFoo.
我想获取IFoo的方法之一pfoo-> Bar()的地址。 Since COM objects are quite well documented, this should be pretty easy.
由于COM对象已被很好地记录,因此这应该很容易。 And in fact, it is.
实际上是这样。 If Bar() is, say, the fifth method in the interface (accounting of course for the IUnknown methods), the address can be extracted from the vtable with
如果说Bar()是接口中的第五种方法(当然要考虑IUnknown方法),则可以使用以下方法从vtable中提取地址:
(*(void***)(pfoo))[5]
My question is, is there a way to do this without having to use "5"? 我的问题是,有没有办法不必使用“ 5”来执行此操作? It seems error-prone to have to count up the methods.
必须累加方法似乎容易出错。 I want to use only the method name.
我只想使用方法名称。 I don't mind if it's a bit complex;
我不介意它是否有点复杂; this is all going in a macro anyway.
无论如何,这都是一个宏。 So my ultimate question is, does there exist a macro that takes the parameters pfoo, Bar, and maybe IFoo, and evaluates to the expression above?
因此,我的最终问题是,是否存在一个宏,该宏带有参数pfoo,Bar以及IFoo,并计算为上面的表达式?
No, there is not a macro to do that. 不,没有宏可以执行此操作。 It's your funeral if you want to do that.
如果要这样做,这是您的葬礼。
Just don't forget that if your interface is derived from IUnknown, then the 5th function is not the 5th function in the interface, but the 8th because before the first method in the interface there is QueryInterface, AddRef, and Release--not necessarily in that order. 只是不要忘记,如果您的接口是从IUnknown派生的,那么第5个函数不是该接口中的第5个函数,而是第8个函数,因为在接口的第一个方法之前有QueryInterface,AddRef和Release-不一定以该顺序。 If you are derived from IDispatch, there is even more methods before your first method.
如果您是从IDispatch派生的,则在第一个方法之前还有更多方法。
If you look at MFC source code, they use some macros to do some similar things in their METHOD_PROLOGUE() macro. 如果您查看MFC源代码,它们将使用一些宏在其METHOD_PROLOGUE()宏中执行一些类似的操作。 It uses either a macro or MS compiler hack titled "offsetof()".
它使用名为“ offsetof()”的宏或MS编译器黑客。 I don't know which it is and am not inclined to look it up, but since MS provides the source code, you should be able to do it.
我不知道它是什么,也不愿意查找它,但是由于MS提供了源代码,因此您应该能够做到。
Edit: I just googled it and it appears that offsetof() is a stddef.h macro. 编辑:我只是谷歌搜索,似乎offsetof()是一个stddef.h宏。 https://en.wikipedia.org/wiki/Offsetof
https://zh.wikipedia.org/wiki/Offsetof
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.