Re: COM Interface intialization

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Steven Edison" <EdisonCPP@xxxxxxxxxxxxxxxxx> wrote in message
news:%23XYleLNOHHA.4604@xxxxxxxxxxxxxxxxxxxx
What I'd like to do is something like this:

STDMETHODIMP CAnotherInterfaceImpl::GetItemInfo(IUnknwn** pUnk)
{
CItemInfo* pInfo = new CItemInfo; //above impl class
pInfo->SetSomeData(0x42); //exposed through impl. class, but
not the interface
//so caller can't
set "somedata", but it can be done here.

The usual idiom goes like this:

CComObject<CItemInfo>* pInfo;
CComObject<CItemInfo>::CreateInstance(&pInfo);
pInfo->AddRef();
pInfo->SetSomeData(0x42);
HRESULT hr = pInfo->QueryInterface(IID_IUnknown, (void**)pUnk);
pInfo->Release();
return hr;

//final use:
CComPtr<IItemInfo> pInfo = NULL;
hr = pAnotherI->GetItemInfo((IUnknown**)&pInfo);

This is illegal. GetItemInfo returns an IUnknown*, you can't just place
it into IItemInfo*. This amounts to a downcast. Instead, you have to
retrieve into IUnknown* pointer, then QueryInterface for IItemInfo. Or,
have GetItemInfo return IItemInfo* in the first place.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.