简体   繁体   English

如何获得COM对象的成员函数的地址?

[英]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.

相关问题 如何使用decltype获取某个类类型的成员函数的地址? - How can I use decltype to get the address of some class type's member function? 我如何从其地址获取成员函数的组成部分? - How can i get constituents of a member function from its address? 如何获取结构成员的地址 - How can I get the address of struct member 我可以使用 C++ 中的成员变量地址获得 object 的引用吗? - Can I get reference of an object with address of member variable in C++? 我该如何解决该成员函数问题重载的地址? - How can I resolve this address of overloaded member function issue? 如何保证对象的寿命与成员函数的持续时间匹配? - How can I guarantee an object's lifespan matches the duration of a member function? 如何将std :: function的目标与成员函数的地址进行比较? - How do I compare a std::function's target with a member function's address? 如何为同一个类对象的成员函数保留单独的变量副本? - How can I keep separate variable copy for same class object's member function? 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 如何获取重载成员function的地址? - How to get the address of an overloaded member function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM