
COM object in a COM object
Hi !
I'm trying to create a COM object with VC++6 and ATL for my company.
From now, I'm just doing tests to understand better how this works
So I write an ATL object with VC++ and I test it under VB6.
Using the class view, I can add simple properties to my object, by
simple, I mean properties of a predefined type like long, short...
Now I created a second object which has its own interface. This gives
me that
CObj1 et IObj1, CObj2 et IObj2
Theses are the names given by VC++. In the registry, and so far for
VB, I have Obj1 and Obj2
I would like Obj1 to contain a reference to an object of type Obj2
that I could call this way from VB :
AnObj1Instance.APropertyOfTypeObj2.AMethodFromObj2
So I added the follozing lines to the IDL file in my project :
[propget, id(4)] HRESULT APropertyOfTypeObj2([out, retval] IObj2
**pVal);
[propput, id(4)] HRESULT APropertyOfTypeObj2([in] IObj2 *newVal);
And I wrote the following functions to access the property
STDMETHODIMP CSGraphic::get_APropertyOfTypeObj2(IObj2 **pVal)
{
*pVal = APointerOfTypeIObj2;
return S_OK;
Quote:
}
STDMETHODIMP CSGraphic::put_APropertyOfTypeObj2(IObj2 *newVal)
{
APointerOfTypeIObj2 = newVal;
return S_OK;
Quote:
}
With APointerOfTypeObj2 declared this way :
IObj2 * APointerOfTypeObj2;
This pointer is initialized in the CObj1 constructor in this way :
HRESULT hr = CoCreateInstance(CLSID_Obj2, NULL, CLSCTX_ALL, IID_IObj2,
(void **)&APointerOfTypeIObj2);
And in the destructor I do this :
APointerOfTypeIObj2->Release();
Now, the Obj2 interface (IObj2) proposes a method (ding) and a long
property (thing)
In VB, I can create instances of Obj2 without any problems and I
access the method and the property without any problem.
Now, If I create an Obj1 and I access any other property than
APropertyOfTypeObj2, everything is ok until the object is destroyed :
VB crashes and if I ask a debug by clicking cancel, I go to the source
of my object on the line where Release is called.
So I decided to suppress this call, even if I think I must keep it.
And after deleting this call I got no more problems in VB.
Well, no more problems with this !
Actually I then tried to access APropertyOfTypeObj2. If i do this :
AnInstanceOfObj1.APropertyOfTypeObj2.ding
Everything is ok and the code in ding (a simple beep) works well.
But as soon as I want to do this
AnInstanceOfObj1.APropertyOfTypeObj2.thing = 2
Or even reading the value of thing,
I got a big error with the message Memory could not be read... and if
I ask for a debug, I got a disassembled version of the DLL...
The strangest thing is that if create an Obj2, accessing the thing
property doesn't create the error.
So I think I made a big error in the way I include Obj2 in Obj1 but I
dont't see what.
I'm searching on the net and the forums but I did'nt find anything
yet.
Thanks in advance for your help