Archives

Archives / 2004 / July
  • Interfaces "Flattened" during COM Export

    While researching a question someone asked, I discovered that TLBEXP will "flatten" out your .NET interface hierarchy.

    Assume you have a simple .NET interface such as:

    public interface IFoo
    {
    	void Method1_v1();
    	void Method2_v1();
    }
    

    When exported to COM, the interface in OLEVIEW looks something like this (only relevant portions shown here):

    interface IFoo : IDispatch {
    	HRESULT Method1_v1();
    	HRESULT Method2_v1();
    };
    

    Later, you want to add more functionality so you define a new interface that inherits from the previous one:

    public interface IFoo2 : IFoo
    {
    	void Method1_v2();
    }
    

    When exported to COM, your two interfaces now appear as:

    interface IFoo : IDispatch {
    	HRESULT Method1_v1();
    	HRESULT Method2_v1();
    };
    
    interface IFoo2 : IDispatch {
    	HRESULT Method1_v2();
    };
    

    You've lost your interface hierarchy. This is a "by design" issue as documented in MSDN:

    All exported interfaces extend directly from either IUnknown or IDispatch, regardless of their inheritance hierarchy in managed code.

    Read more...