开发者

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

开发者 https://www.devze.com 2023-02-27 00:48 出处:网络
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

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

精彩评论

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