Link to home
Start Free TrialLog in
Avatar of Kerplunk
Kerplunk

asked on

Objects in COM

Is there a way to pass a non-COM object (an object created outside of the COM object) through an interface where we do not have to know the definition of the object. Kind of along the line of this:


Outside of the COM object, we have a C++ class:
class Foo
{
    // methods, variables, etc.
}

In the .idl file for the COM object, we have this:
[id(1), helpstring("method Test")] HRESULT Test( blah );

Is there a way to pass an instance of class Foo into the COM object through the interface method Test, with the ability to use the methods and variables within the Foo object from the COM object?

What would the parameters in the instance method be?
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

include the class in the COM project
pass the Foo's pointer as unsigned long ie define method Test with parameter ulong
from the client pass the class pointer by casting it into ulong.

One thing u should take care is that,
if the class contains any read write operation, or any other synch needed operation, u should have to handle this...

GOOD LUCK
Avatar of Kerplunk
Kerplunk

ASKER

But what if we don't have the header file or any other file for the class, so we don't know what the definition is?

In Java I know we can use the Object class, since every new class is derived from it. Is there an equivalent in COM, so we can use a class without having to know what its structure is before hand?
In COM, all interfaces must derive from the IUnknown interface, and implement the 3 methods within that interface; QueryInterface, AddRef, and Release.  I don't know Java, but your statement about the Object class seems to parallel COM in this respect.

roshmon's suggestion is similar to using a void* in C/C++.  You can use a void* to hold onto any object, but if you want to access that object, eventually you'll have to cast it to the appropriate type (class).  This means you'll need the type definition.  Can't do much (reliably) without it.
Dear Kerplunk

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to split between

     "roshmon and boneTKE"


If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: https://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
I didn't forget about the question, I've just been trying to figure out how to solve it.

roshmon and boneTKE did answer one part of the question (sending an object into COM). However, after some reading, I found what I was looking for for the second part (using methods without the definitions). What I am looking for is called reflection (or introspection). This is where a class knows about its methods and variables, and can be seen in the Java reflection API (and the new .NET platform) because they both go off of a single root class (Object). It would seem that, to get this functionality in C++, a new class or interface would need to be created to simulate these effects, and used by classes outside of my project that are to be passed to the COM object.
ASKER CERTIFIED SOLUTION
Avatar of Mindphaser
Mindphaser

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