Link to home
Start Free TrialLog in
Avatar of Z_Beeblebrox
Z_Beeblebrox

asked on

Implement multiple COM interfaces

Hi,

I was wondering if it is possible, and if so, how, to implement multiple COM interfaces which contain methods with the same signatures. I am using VC++, but when I try to add a method to a class' main interface via the wizard that has the same signature as a method in an interface implemented by that class, I get an error saying that the method already exists. The COM interfaces implemented by a COM class are supposed to be independent, so there must be a way to handle this. (Note that I want different implementations of the methods depending on which interface it is being called on).

TIA,
Zaphod.
Avatar of _corey_
_corey_

Well, first of all, why would you want something so confusing?
Avatar of Z_Beeblebrox

ASKER

Well, for one thing I don't necessarily have control over what methods are defined in interfaces. Also, I am creating a component which performs two different tasks (thus two interfaces) but both of those tasks involve a similar operation (similar from the client's perspective, different implementation). But regardless, I can't ensure that no two interfaces that I may want to implement do not duplicate method signatures. Many of my classes already implement multiple interfaces (IUknown, ISupportsErrorInfo, IObjectControl,...) so this is not exactly an unusual thing to do.

Zaphod.
Avatar of jkr
>>but when I try to add a method to a class' main interface via the wizard that has the same signature as a >>method in an interface implemented by that class, I get an error

Well, that's not really surprising. You cannot have more than one method with the same signature, how should the compiler know which one you were referring to? That's why you would use aggregation - see e.g.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/htm/com_9xym.asp ("Aggregation")
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncomg/html/msdn_house4.asp ("MFC/COM Objects 4: Aggregation")
http://www.microsoft.com/msj/archive/S3E9.aspx ("How COM Solves the Problems of Component Software Design, Part II ")
I think if class implements two interfaces having the same function, this class implements such function only once, and it may be accessed using both interfaces. If two interfaces have the same function, but this function has different meaning in them, possibly these interfaces are completely unrelated and implementing of them in one COM component has no sence.
Yes, definately by using aggregation, you are able to explicitely pass back the whole valid interface that the user is requesting from the object.

There is really nothing different with these interfaces composed in the object, they inherit IUnknown/etc.  The way you query them is the same, the main difference is your implementation of QueryInterface which passes back the interface variable explicitely.
jkr: Do I have to use aggregation? It seems needlessly complicated for such a simple problem. Really, all I need to do is to somehow map the interface method to an internal method with a different name. This is possible in .Net, is it not possible in VC++ COM?
AlexFM: Your first sentence is true for standard OOP interfaces, but not for COM interfaces. In COM, when calling a method on an object, you must specify which interface you are using, so from the client's persective there is clear separation between methods on different interfaces, even if they have the same signature. jkr's third link describes the use of multiple interfaces in COM very well.

Zaphod.
ASKER CERTIFIED SOLUTION
Avatar of _ys_
_ys_

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
So you're saying as long as the interface method signature is the same, you can cast to it.
As long as the entire interface signature is the same - not just a singular method - you can cast to it.
Well, yea, since we were talking about single-method interfaces, I implied that without voicing it :)
Thanks _ys_, you answered my question. Unfortunately, doing something that like would make the code too hard to understand and maintain, so instead I am going to have to change the interfaces to have different methods.

Zaphod.
Some day you may have to - Don't forget that after a COM interface is in the wild (published) it's immutable. Any alterations to it are not happening.