COM:使用实现接口的指针获取coclass对象的GUID [英] COM: Getting GUID of coclass object using pointer to interface it implements
问题描述
具有由某些具体组件类对象实现的COM接口的指针,是否有可能获得实现该接口的基础对象的GUID(CLSID)?
Having pointer to COM interface that are implemented by some concrete component class object is it possible to get a GUID of the underlying object that implements this interface (CLSID)?
更一般而言,我有一个类似 SetFont(ICanvasFont * font)
的函数,我需要一种方法确定实现 ICanvasFont
接口的基础对象是否属于某个类(例如 MCanvasFont
)。
More generally, I have a function like SetFont(ICanvasFont* font)
and I need a way to determine if the underlying object that implements the ICanvasFont
interface is of a certain class (say MCanvasFont
).
推荐答案
此接口指针上的IUnknown :: QueryInterface
可获得以下之一: IPersist
, IPersistStream
, IPersistStreamInit
或其他 IPersist *
接口。如果您很幸运获得一个,则 GetClassID
方法将为您提供 CLSID
类标识符(替代选项为 IProvideClassInfo
和 IProvideClassInfo :: GetClassInfo
)。
IUnknown::QueryInterface
on this interface pointer to obtain one of the following: IPersist
, IPersistStream
, IPersistStreamInit
or other IPersist*
interfaces. If you are lucky to get one, then GetClassID
method will get you the CLSID
class identifier (alternate option is IProvideClassInfo
and IProvideClassInfo::GetClassInfo
).
这类信息不必存在。接口指针可以有效,而在实现它的类上没有 CLSID
。
Note that this kind of information does not have to exist. An interface pointer can be valid without having CLSID
on the class implementing it.
UPD。如果主要目标是在提供的接口上识别您自己的实现(提供的 ICanvasFont
是我自己的 MCanvasFont $ c $的实例c>类,还是有所不同?),那么最简单而有效的方法是在类上实现一些额外的私有接口。如果查询成功,则可以识别该实例。如果没有封送处理,您甚至可以
static_cast
回到原始C ++指针。
UPD. If the main goal is to recognize your own implementation on the provided interface ("Is the provided ICanvasFont
the instance of my own MCanvasFont
class, or it is something different?"), then the easiest yet efficient way is to implement some extra private interface on the class. If your querying it succeeds, then you recognize the instance. Provided no marshaling takes place, you can possibly even static_cast
back to original C++ pointer.
这篇关于COM:使用实现接口的指针获取coclass对象的GUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!