Property that sets and gets an object
Hello everybody
I'm developing al litle control in VC++2005 it's my first time with ATL
an I'm lost in a little question.
The control has two classes with its interfaces (CContainer=IContainer,
CContent=IContent). The first one has defined a property that gets or
sets the second object.
The definition on the .idl file is like this:
--BEGIN CODE--
[...]
interface IContent : IDispatch{
[id(1), helpstring("method Hello")] HRESULT Hello([out,retval] BSTR* ret);
};
[...]
interface IContainer: IDispatch{
[propget, id(2), helpstring("...")] HRESULT Content([out, retval]
IContent ** cont);
[propput, id(2), helpstring("...")] HRESULT Content([in] IContent * cont);
};
(...)
--END CODE--
The CContainer class has an CContent object defined as:
--BEGIN CODE--
CComObject<CContent> * cContent;
--END CODE--
An that I initialize in the constructor like this:
--BEGIN CODE--
cContent= new CComObject<CContent>;
--END CODE--
Don't ask about why I do this because exactly I don't have any idea. I
have copied a sample in some web.
All compiles well, the object is created and I can access to all the
functions in it.
When I use the object in VB6 al can open the object without problems.
The problem begins when I try to reuse the object cContent from the VB6.
Initially I defined the function get as:
--BEGIN CODE--
STDMETHODIMP IContainer::get_Content(IContent** pVal)
{
IContent* pDisp;
cContent->QueryInterface(IID_IContent, (void**)&pDisp);
ATLASSERT(pDisp);
*pVal = pDisp;
return S_OK;
}
--END CODE--
When I use the object in VB as follow I don't have any problem
--BEGIN CODE--
Dim a As ContainerLib.Container
Set a = New Container
a.Content.Hello
--END CODE--
It access to the object without any problem and executes the method
'Hello' correctly. But when I try to access by a second time to the
object all crashes at the line of the QueryInterface.
I' don't know what's the correct procedure to return an interface.
Somebody can help me with this?
And a second question.
How can I implement the 'set_Content(IContent* pVal)' function to set
the cContent object to the passed IContent *pVal?
Thank you in advance,
David Cases