开发者

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

I want to try and modify my code to use a superclass to handle creating CComPtr, but I'm not sure how to pass the class to the CComPtr to create,开发者_Go百科 ie the part in

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);
}


Depending on what you want to achieve, templates can do the job:

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


I think you can use IIDFromString function to get an Interface Id and then do a QueryInterface on that. Create the COM Object on IUnknown and then do a QueryInterface on your newly-resolved IID.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜