如何在超类中设置CComPtr上的接口

How do I set the interface on CComPtr in a Superclass?

本文关键字:CComPtr 接口 设置 超类      更新时间:2023-10-16

我想尝试修改我的代码使用超类来处理创建CComPtr,但我不确定如何将类传递给CComPtr来创建,即

中的部分
void CSuperClass::CreateSmartPointer(CString class, Interface interface)
{
   CLSID clsid;
   hr = CLSIDFromProgID(class, &clsid);
   CComPtr<interface> spInterface;
   hr = spInterface.CoCreateInstance(clsid, 0, CLSCTX_ALL);
}

void CSubClass::Init()
{
    CreateSmartPointer("MYServer.MyClass", xxx);
}
void CSubClass2::Init()
{
    CreateSmartPointer("MYServer2.MyClass2", xxx);
}

根据您想要实现的目标,模板可以完成任务:

template<class Interface> class CSuperClass { 
    // ...
    void CreateSmartPointer(CString class) {
        // ...
        CComPtr<Interface> spInterface;
        // ....

我认为您可以使用IIDFromString函数来获取接口Id,然后在该接口上执行QueryInterface。在IUnknown上创建COM对象,然后在新解析的IID上创建QueryInterface