Ambiguity of Common Interface in two COM component when generating RCW by VS2005  
Author Message
GeminiSaka





PostPosted: Visual C++ General, Ambiguity of Common Interface in two COM component when generating RCW by VS2005 Top

My situation is described as below:

1. Define an interface in "CommonInterface.idl":

interface Interface1 : IUnknown {
[, helpstring("method Add")] HRESULT Add([in] int a1, [in] int a2, [out] int *sum);
};

Compile it to generate corresponding .h, _i.c, _i.p files; (I do not know how to compile a sole .idl file, so I create a ATL project, write the definition of interface in .idl flieand compile it)

2. Create two COM component by ATL project, both use the Interface1:

"SampleCOM1.idl":

library SampleCOM1Lib
{
import "CommonInterface.idl"; // import idl file defined Interface1

importlib("stdole2.tlb");
[
uuid(EBBD4C25-4C46-4355-B0DA-586945ACD464),
helpstring("Sample1 Class")
]
coclass Sample1
{
[default] interface ISample1;
interface Interface1; // coclass Sample1 implements Interface1
};
};

"SampleCOM2.idl":

library SampleCOM2Lib
{
import "CommonInterface.idl"; // import idl file defined Interface1


importlib("stdole2.tlb");
[
uuid(37262053-20FF-4ECC-96F3-C530D7E078C3),
helpstring("Sample2 Class")
]
coclass Sample2
{
[default] interface ISample2;
interface Interface1; // coclass Sample2 implements Interface1
};
};

3. Build the two ATL projects and get "SampleCOM1.dll" and "SampleCOM2.dll"

4. Add the two COMs into a C# console project's references and generate:

Interop.SampleCOM1Lib,

Interop.SampleCOM2Lib,

both contain the interface Interface1, namely there're two Interface1:

SampleCOM1Lib.Interface1 and SampleCOM2Lib.Interface1, so ambiguity arises.

In my opioion, there should be only one Interface1 as a common interface, and class Sample1 and Sample2 contains the different implementation towards Interface2. How to make it come true

In SampleCOM1.idl (or SampleCOM2.idl), there's no definition of Interface1, just the coclass implements it, why In SampleCOM1Lib (or SampleCOM2 ) there's the Interface1 But where the Interface1 should be



Visual C++2  
 
 
Dreedle





PostPosted: Visual C++ General, Ambiguity of Common Interface in two COM component when generating RCW by VS2005 Top

Where are the interface definitions for ISample1 and ISample2



 
 
GeminiSaka





PostPosted: Visual C++ General, Ambiguity of Common Interface in two COM component when generating RCW by VS2005 Top

The definition of ISample1 and ISample2 are in SampleCOM1.idl and SampleCOM2.idl respectively. There're no methods in the two Interface, they exist just when create object using ATL wizard. So, they are the default interfaces for coclass Sample1 and Sample2 respectively.
 
 
Dreedle





PostPosted: Visual C++ General, Ambiguity of Common Interface in two COM component when generating RCW by VS2005 Top

My COM is rusty to say the least - and my ATL non-existant - never liked ATL at all - but here goes anyway...

From your IDL:

1. Your definition of Interface1 is missing it's IID ( uuid attribute ) - generate a GUID and add the uuid atribute to the interface definition. Suprised that got past the IDL compiler - but it may be a cut and paste error.

2. If you do not intend to add methods or properties to ISample1 and ISample2 interfaces, remove them from your coclasses you do not need them. If you keep them AND they directly derive from Interface1 then remove Interface1 from your coclass definitions. This is where ambiguity can arise if you're telling the IDL compiler that ISample1 and ISample2 interface inherits (and therefore must provide an implementation of) Interface1, and then declaring Interface1 again in the coclass you are saying that another implementation of Interface1 must be provided by each coclass - that is, each coclass is exposing two implementations of Interface1. Only 'top level' interfaces need to be defined in the coclass definition. Interfaces that have been derived from need not.

Hope that all makes sense! It may not be your problem as I am unfamiliar with ATL and how it generates the boilerplate code but the above (if memory serves me correctly) is true for IDL definitions.



 
 
GeminiSaka





PostPosted: Visual C++ General, Ambiguity of Common Interface in two COM component when generating RCW by VS2005 Top

Sorry, maybe I did not describe my situation clearly. Interface1 acctually has its IID and I just ignore it here.

I've made the test, that SampleCOM1Lib.Interface1 and SampleCOM2Lib.Interface1 can convert to each other using explicit type conversion and both of them acctually refer to the Interface1 defined in CommonInterface.idl because they share the same IID.

But the problem is how can I custom the generation of RCW to make the interface or class only declared in designated namespace, for Interface1, I only want it at the CommonInterfaceLib namespace but not in SampleCOM1Lib or SampleCOM2Lib, so there's no the boring type conversions.