Tuesday, October 27, 2009

ATL Removal: Part 6 – Replacing CComQIPtr with _com_ptr_t

ATL Removal: Part 6 – Replacing CComQIPtr with _com_ptr_t
In part 4 of ATL removal, I talked about removing CComPtr and replacing it with _com_ptr_t. You may run into some smart pointers in the form of CComQIPtr. CComPtr and CComQIPtr have the same base class and CComQIPtr is a super set of CComPtr. I tried to read up on CComQIPtr in MSDN, but the documentation wasn’t really very helpful. In the code I am working on, CComQIPtr is used to create a smart pointer and then do a QueryInterface hence the QI. I verified this is what goes on by looking through the atlbase.h header file from the SDK.

If you don’t want to depend on ATL in the sense that your binary doesn't load the ATL dlls, the it is fine to still include the .h file, but if you want to purge ATL altogether, read on. Keep in mind _com_ptr_t doesn't do everything that ATL smart pointers, so some extra manual work may still be required. This is an example of a case where the extra legwork is required.

CComQIPtr(IUnknown* lp)
{
p=NULL;
if (lp != NULL)
lp->QueryInterface(*piid, (void **)&p);
}

Before I had some code that looked like this:
CComQIPtr spLocaclVariable( m_spMemberProperty);

To fix it, I changed it to this:
_com_ptr_t<_com_IIID< IYourInterface, &__uuidof(IYourInterface)> > spLocaclVariable;
if (spMemberProperty)
{
spMemberProperty ->QueryInterface(__uuidof(IYourInterface), (void**)& spLocaclVariable);
}

I think this should do the trick for your CComQIPtr smart pointers.

1 comment:

  1. This is a misplaced comment, but I cannot find your email address anywhere, so try to contact you here.

    I'm interested in having a look at your stuff at smadasam.googlepages.com/gpufdtdcode, but the links there don't work

    ReplyDelete