General tlbimp output questions

Jul 28, 2009 at 12:19 AM

Given the IDL definition:

[
   object,
   uuid(SOME_GUID),
   dual, 
   oleautomation, 
   helpstring("IFoo Interface"),
   pointer_default(unique)
]
interface IFoo : IUnknown
{
   HRESULT Foomethod([in]BSTR aString);
};

[
   uuid(SOME_OTHER_GUID),
   helpstring("Foo Class")
]
coclass Foo
{
   [default] interface IFoo;
};

The interop generated from this IDL will generate 1 interface definition (IFoo), 1 class definition (FooClass) and 1 (for lack of better term) hybrid (Foo) that is an interface but can be created.  
Valid ways of creating the Foo CoClass would be:
IFoo foo1 = new FooClass( );
IFoo foo2 = new Foo( );
Foo foo3 = new FooClass( );
Foo foo4 = new Foo( );
Questions:
Is any of these initialization expressions better than any other?
Why is the hybrid type definition generated at all?
Is there a way to suppress the generation of the hybrid type definition?
Is there a way have methods that take an IFoo interface as a parameter in the IDL take an IFoo interface in the interop assembly instead of the hybrid type definition?
If tlbimp doesn't do this is there a way to have tlbimp2 do this?
Thanks for any help on this.
Developer
Jul 29, 2009 at 9:35 AM

Is any of these initialization expressions better than any other?

IFoo foo1 = new FooClass( );
IFoo foo2 = new Foo( );
Foo foo3 = new FooClass( );
Foo foo4 = new Foo( );
//compiler will do a lot of stuff to translate the CoClass(new Foo). but  the  generated IL Code is same for the above 4 cases( newobj instance void *Lib.FooClass::.ctor()).

so they are equal if you dont carethe work of compiler.

Why is the hybrid type definition generated at all?

The original reason is to avoid the confusion of dealing with renamed classes, so user can write code to instantiate an "interface".

Is there a way to suppress the generation of the hybrid type definition?

not yet. There is no way to do so except manually.(both for tlbimp and tlbimp2)

4. Is there a way have methods that take an IFoo interface as a parameter in the IDL take an IFoo interface in the interop assembly instead of the hybrid type definition?

The default behavior: if the ifoo interface isn't default interface, then we will keep it the original one IFoo. but if the ifoo is default interface, we will translate the ifoo into coclass instead
if you want to change the default behavior, you can use other tools, such as tlbimp customization.

5. If tlbimp doesn't do this is there a way to have tlbimp2 do this?

Not sure what the "do" mean?

Jul 29, 2009 at 4:42 PM

Thanks for the response and you answered the 'do' question, I meant is there a way to suppress the 'hybrid' type. 

Its kind of interesting you mentioned the 'hybrid' (BTW is there a better name for it) was added to avoid confusion since it has done the opposite for myself and several other developers I work with.

Developer
Jul 30, 2009 at 7:45 AM

Its kind of interesting you mentioned the 'hybrid' (BTW is there a better name for it) was added to avoid confusion since it has done the opposite for myself and several other developers I work with.

Yes. there is a name for this.  For example. Foo is coclass interface, FooClass is RCW Class.  About the opposite result. we are very sorry for this and maybe improve or change this in the furture release.