Solved

CComObject

Posted on 2002-06-18
10
516 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error on moodle after upgrade 3 123
Unix / Linux grid computing 5 149
java ^ examples 8 62
Is there a simple front-end menu system. 9 71
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

896 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

16 Experts available now in Live!

Get 1:1 Help Now