title: C# COM Interface Inheritance
body: <p>I have the following (DirectShow related) COM interface in C++ that I need to call in C#.</p>
  
  <pre><code>[uuid("...")] : public IUnknown
  interface IBar
  {
      STDMETHOD(Bar)(void);
  }
  
  [uuid("...")]
  interface IFoo : public IBar
  {
      STDMETHOD(Foo)(void);
  }
  </code></pre>
  
  <p>I declared this in C# as the following but it crashes out with Access Violation when IFoo.Foo() is called. Works fine with IBar.Bar() is called though. What is the correct way to write the equivalent interface in C#?</p>
  
  <pre><code>[ComImport, Guid("...)]
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  [SuppressUnmanagedCodeSecurity]
  public interface IBar
  {
      [PreserveSig] int Bar(); // Calling this is OK
  }
  
  [ComImport, Guid("...)]
  [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
  [SuppressUnmanagedCodeSecurity]
  public interface IFoo : IBar
  {
      [PreserveSig] int Foo(); // Calling this crashes
  }
  </code></pre>
  
  <p>The vtables must be incompatible in C# vs C++ for inherited COM interface. Is there a way to tell C# to adhere to the C++ declaration?</p>