Link to home
Start Free TrialLog in
Avatar of stewe
stewe

asked on

CComObject

 Hi !

I receive an IDispatch* in a COM object's method. I know what object should it be.
I would like to acces it's ATL class so I can manipulate it.
Details:

class ATL_NO_VTABLE CMyObj :
   public CComObjectRootEx...
   ...

STDMETHODIMP COtherObj::Manipulate(IDispatch *ptr)
{
   CMyObj *obj=???ptr???
   obj->m_iY=1;
   obj->Manipulate(...);
}

Maybe using CComObject or something like that.

  Stewe
Avatar of migel
migel

Hi!
You can`t directly cast IDispatch pointer to the your object since you can receive anything else.
You can add internal interface (not registered in the TLB) to the your class and manupulate it by this interface
Hi!

(Don't really know what migel means - need not to be wrong, though -, but)
I would make it this way:

STDMETHODIMP COtherObj::Manipulate(IDispatch *ptr)
{
  IMyObj *obj;
  ptr->QueryInterface(IID_IMyObj, (void**)&obj);
  obj->m_iY=1;
  obj->Manipulate(...);
}
Avatar of stewe

ASKER


This is the way that I'm using now.
But. m_iY is not a COM property but a C++ class property.
So I need
  class MyObj*
not
  IMyObj *
Is it possible ?
>>>> So I need class MyObj*  not IMyObj * Is it possible ?

No, it's not (that's what miguel wanted to say).

The design of COM limits you to using the interface, since you don't know for sure what "hides behind" the IMyObj*. In fact, you could even get it working undercertain conditions, but I could break it with one additional registry entry.
Thank you peter! my English not so guud :-)
Avatar of stewe

ASKER

What do you mean 'I could break it with one additional registry entry' ?
HI!
By this phrase, Peter mean that any user can implement your interface and pass such object to you. So unpredictable result can occure.
ASKER CERTIFIED SOLUTION
Avatar of peterchen092700
peterchen092700

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stewe

ASKER

Yea. I do it.
I keep CMyObj in-touch, f.e. mapped, and look up with a string property as key. So I use that IDispatch (IMyObj) to get the key value, then look up.

and... it works.

   Thx
>> I keep CMyObj in-touch, f.e. mapped, and look up with a string property as key.
perfect!