Solved

CComObject

Posted on 2002-06-18
10
527 Views
Last Modified: 2013-11-20
 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
0
Comment
Question by:stewe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 12

Expert Comment

by:migel
ID: 7088347
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
0
 
LVL 2

Expert Comment

by:Acrklor
ID: 7091044
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(...);
}
0
 
LVL 1

Author Comment

by:stewe
ID: 7092035

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 ?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:peterchen092700
ID: 7092417
>>>> 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.
0
 
LVL 12

Expert Comment

by:migel
ID: 7092529
Thank you peter! my English not so guud :-)
0
 
LVL 1

Author Comment

by:stewe
ID: 7092561
What do you mean 'I could break it with one additional registry entry' ?
0
 
LVL 12

Expert Comment

by:migel
ID: 7092599
HI!
By this phrase, Peter mean that any user can implement your interface and pass such object to you. So unpredictable result can occure.
0
 
LVL 7

Accepted Solution

by:
peterchen092700 earned 200 total points
ID: 7092652
The main point is that casting the IMyObj to a CMyObj is against the "contract" that the client agreed on when using IMyObj. It's relying on something that isn't guaranteed.

>> I could break it with one additional registry entry
This is only an illustration what could happen:

I could replace your MyObj implementation with a similar that is implemented in a completely different way - or even a completely different language.

I could force the MyObj component to always run in an surrogate process (which is a usual way to avoid one faulty component to break down the entire application).

I could force MyObj always to be created on a remote server.

All these things can be achieved without touching your binaries, just by changing some registry settings.

The problem in these cases is: your client will get a CMyObj* as well, but the pointer is utterly meaningless, accesing it most likely causes an access violation, and there's no one-step check that your CMyObj pointer is valid.

------------------

Alternatives:
I know that passing all information through a COM interface is harsh.
The only alternative would be the client (the module that *receives* the IDispatch *) to be the one that also creates the MyObj instance, and keeps both the CMyObj* and the IMyObj* around.

Peter
0
 
LVL 1

Author Comment

by:stewe
ID: 7092730
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
0
 
LVL 7

Expert Comment

by:peterchen092700
ID: 7092770
>> I keep CMyObj in-touch, f.e. mapped, and look up with a string property as key.
perfect!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question