Problem using common interface included in two typelib's 
Author Message
 Problem using common interface included in two typelib's

Hi,

I've defined a basic interface (IShape) to be used in multiple
projects/dlls in a small idl file. Since it is just an interface and
not a library, it cannot compile to a tlb file.

This interface is used by two different projects/dlls (aaa.dll and
bbb.dll), both using the import statement in the idl file. This works
fine without any problems.

The problem occurs when I make an application that uses the basic
interface (IShape) and both dlls. The application gets access to the
dll objects/functions using the #import directive for both dlls in the
.cpp file. This leads to two imports of IShape called AAA:IShape and
BBB:IShape.

Now, I would have expected the compiler to realize that this is
actually the same type and to allow me to use them interchangably.
However, this is not the case because I get compiler error C2664 every
time I try to pass AAA:IShape to a function i BBB or vice versa.

But the two types (AAA::IShape and BBB::IShape) types are actually
identical because it works if I explicitely do a QueryInterface
between the two types.

So my question is, how can I make the compiler realize that these two
types are identical and allow me to use them interchangably? Or should
I include/import the basic types differently to make it work?

Thanks in advance!
Ulrik



Sat, 26 Feb 2005 15:31:45 GMT  
 Problem using common interface included in two typelib's

Quote:

> Hi,

> I've defined a basic interface (IShape) to be used in multiple
> projects/dlls in a small idl file. Since it is just an interface and
> not a library, it cannot compile to a tlb file.

> This interface is used by two different projects/dlls (aaa.dll and
> bbb.dll), both using the import statement in the idl file. This works
> fine without any problems.

> The problem occurs when I make an application that uses the basic
> interface (IShape) and both dlls. The application gets access to the
> dll objects/functions using the #import directive for both dlls in the
> .cpp file. This leads to two imports of IShape called AAA:IShape and
> BBB:IShape.

> Now, I would have expected the compiler to realize that this is
> actually the same type and to allow me to use them interchangably.
> However, this is not the case because I get compiler error C2664 every
> time I try to pass AAA:IShape to a function i BBB or vice versa.

> But the two types (AAA::IShape and BBB::IShape) types are actually
> identical because it works if I explicitely do a QueryInterface
> between the two types.

> So my question is, how can I make the compiler realize that these two
> types are identical and allow me to use them interchangably? Or should
> I include/import the basic types differently to make it work?

> Thanks in advance!
> Ulrik

First - I'm no expert!  But I have needed to do a similar thing with
multiple instantiations of the same interface.

The idl file _can_ be compiled to a library, but you need to create the
LIB section in the idl file.  In this section simply reference the
interface, e.g.:

*******************************************
import "oaidl.idl";
import "ocidl.idl";

        [
                object,
                uuid(<some guid>),
                helpstring("ILMData Interface"),
                pointer_default(unique)
        ]
        interface ILMData : IUnknown
        {
                // lots of methods!
        };

[
        uuid(<some other guid>),
        version(1.0),
        helpstring("LMData Interface 1.0 Type Library")
]
library LMDATALib
{
        importlib("stdole32.tlb");
        importlib("stdole2.tlb");

        interface ILMData;

Quote:
};

*********************************

This compiles as normal, and can then be imported _once_ to get the
interface definition.  In my application, I don't import the DLLs at all
- you just need a method for looking up the CLSIDs (e.g. registry).

HTH



Sat, 26 Feb 2005 17:41:35 GMT  
 Problem using common interface included in two typelib's
One more step - the other DLLs' IDL files need to add an importlib()
statement for the TLB generated by the dhared IDL. Then in your
client code you #import the shared TLB too.

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD

MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================

Quote:


> > Hi,

> > I've defined a basic interface (IShape) to be used in multiple
> > projects/dlls in a small idl file. Since it is just an interface and
> > not a library, it cannot compile to a tlb file.

> > This interface is used by two different projects/dlls (aaa.dll and
> > bbb.dll), both using the import statement in the idl file. This works
> > fine without any problems.

> > The problem occurs when I make an application that uses the basic
> > interface (IShape) and both dlls. The application gets access to the
> > dll objects/functions using the #import directive for both dlls in the
> > .cpp file. This leads to two imports of IShape called AAA:IShape and
> > BBB:IShape.

> > Now, I would have expected the compiler to realize that this is
> > actually the same type and to allow me to use them interchangably.
> > However, this is not the case because I get compiler error C2664 every
> > time I try to pass AAA:IShape to a function i BBB or vice versa.

> > But the two types (AAA::IShape and BBB::IShape) types are actually
> > identical because it works if I explicitely do a QueryInterface
> > between the two types.

> > So my question is, how can I make the compiler realize that these two
> > types are identical and allow me to use them interchangably? Or should
> > I include/import the basic types differently to make it work?

> > Thanks in advance!
> > Ulrik

> First - I'm no expert!  But I have needed to do a similar thing with
> multiple instantiations of the same interface.

> The idl file _can_ be compiled to a library, but you need to create the
> LIB section in the idl file.  In this section simply reference the
> interface, e.g.:

> *******************************************
> import "oaidl.idl";
> import "ocidl.idl";

> [
> object,
> uuid(<some guid>),
> helpstring("ILMData Interface"),
> pointer_default(unique)
> ]
> interface ILMData : IUnknown
> {
> // lots of methods!
> };

> [
> uuid(<some other guid>),
> version(1.0),
> helpstring("LMData Interface 1.0 Type Library")
> ]
> library LMDATALib
> {
> importlib("stdole32.tlb");
> importlib("stdole2.tlb");

> interface ILMData;
> };
> *********************************

> This compiles as normal, and can then be imported _once_ to get the
> interface definition.  In my application, I don't import the DLLs at all
> - you just need a method for looking up the CLSIDs (e.g. registry).

> HTH



Sun, 27 Feb 2005 01:55:49 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. IDL problem : setting Interface inside or outside typelib / Proxy stub vs Aggregation

2. Two interfaces inherit from same interface problem

3. Interface disappears from Typelib and IDL file...

4. problem invoke Atl com with two interfaces

5. What's wrong with the TYPELIB?

6. Using external library's interfaces as arguments

7. Can't get the right interface pointer using VBA with my ATL Controls

8. Why won't a include file get included some times and not other

9. Can't import com interface using #import

10. VB6 can call into typelib generated using VC7?

11. TWO SMALL PROBLEMS AND I'M DONE!

12. newbie's answer to two nested for loops problem

 

 
Powered by phpBB® Forum Software