
Passing a Pointer to Pointer to an Interface
See inline
-------------
Daniel Bary3a
Quote:
> Hi,
> I have the following defenitions of the two interfaces to be used by a VB
> client ,I have two questions,
> a)should I create the ITest interface using CoCreateInstance or can I use
> CComPtr?
> b)should I do an AddRef on the newly created ITest interface * when
passing
> the interface?
> interface ITestCreator : IDispatch
> {
> [id(1), helpstring("method callme")]HRESULT callme([out,retval] ITest **
> testObject);
> };
> interface ITest : IDispatch
> {
> [propget, id(1), helpstring("property testid")] HRESULT testid([out,
> retval] long *pVal);
> [propput, id(1), helpstring("property testid")] HRESULT testid([in] long
> newVal);
> };
> say in the implementation of
> STDMETHODIMP CTestCreator::callme(ITest ** test)
> {
// You can use raw pointer ...
::CoCreateInstance(CLSID_Test, NULL, CLSCTX_ALL, IID_ITest, (void
**)test);
// and call interface methods if you'd like ...
// i.e. test->someInterfaceMethod(...)
// ... or CComPtr<> ...
CComPtr<ITest> ptrTest;
ptrTest.CoCreateInstance(CLSID_Test);
// and call interface methods if you'd like ...
// i.e. test->someInterfaceMethod(...);
*test = ptrTest.Detach();
// ... or CComObject<> ...
CComObject<CTest>* pTest = NULL;
CComObject<CTest>::CreateInstance(&pTest);
pTest->AddRef();
// ... and call methods or set members of a class which implements
ITest interface
// i.e. pTest->m_someIntMember = 5, pTest->someMethod(...)
pTest->QueryIterface(IID_ITest, (void **)test);
pTest->Release();
// I prefer he last one method - of course if
// ITest and ITestCreated are located in the same library -
// if not use first or second approach
Quote:
> }