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
Solved

CComObject

Posted on 2002-06-18
10
520 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 99
Windows Drag & Drop Location 2 108
EvenOdd challenge 10 126
FizzBuzz challenge 9 82
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. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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