Solved

CComObject

Posted on 2002-06-18
10
511 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WinWaitActive parameters 12 31
wait notify demo infinite loop 3 81
sumHeights  challenge 17 61
Problem to event 3 50
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now