COM object in a COM object 
Author Message
 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



Sun, 11 Jan 2004 02:33:20 GMT  
 COM object in a COM object
You forgot the reference counting:

STDMETHODIMP CSGraphic::get_APropertyOfTypeObj2(IObj2 **pVal)
{
        *pVal = APointerOfTypeIObj2;
       *pVal->AddRef(); // [out] parameters must be AddRef'ed
        return S_OK;

Quote:
}

STDMETHODIMP CSGraphic::put_APropertyOfTypeObj2(IObj2 *newVal)
{
        if (APointerOfTypeIObj2)
            APointerOfTypeIObj2->Release();
        APointerOfTypeIObj2 = newVal;
        APointerOfTypeIObj2->AddRef(); //{*filter*} on to an [in] parameter
        return S_OK;

Quote:
}

--
With best wishes,
    Igor Tandetnik


Quote:
> 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;
> }

> STDMETHODIMP CSGraphic::put_APropertyOfTypeObj2(IObj2 *newVal)
> {
>         APointerOfTypeIObj2 = newVal;
>         return S_OK;
> }

> 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



Sun, 11 Jan 2004 03:33:42 GMT  
 COM object in a COM object
Thanks a lot, now it works perfectly !

Best Wishes
OBones



Sun, 11 Jan 2004 08:27:57 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Return a pointer to a nested COM object from a COM object method

2. Problem with scripting context when using VB com object inside VC com object

3. Invoking COM object from COM object

4. passing COM object pointers to another COM object in ATL

5. COM Object returning COM Object Reference

6. Passing COM objects as parameters to other COM objects

7. C++ COM-Object with COM-Object as parameter

8. Creating COM objects from COM objects

9. Passing COM objects as parameters to other COM objects

10. Using COM objects within COM objects

11. ASP COM object with ASP COM object as parameter?

12. How to correctly release the COM object inside other COM object?

 

 
Powered by phpBB® Forum Software