Search code examples
c#com-interop

Enumerate list of COM interface implemented through C# COM Interop


I'm querying an MFC implemented COM object that is implemented as follows:

class CA :
   public MfcComLib::IA
{
   ...
};

class CB :
   public MfcComLib::IB
{
   ...
};

class ATL_NO_VTABLE CExposedCoClass : 
   public CComObjectRootEx<CComSingleThreadModel>,
   public CA,
   public CB

{
public:

BEGIN_COM_MAP(CExposedCoClass )
   COM_INTERFACE_ENTRY(MfcComLib::IA)
   COM_INTERFACE_ENTRY(MfcComLib::IB)
END_COM_MAP()

On the C# side I'm receiving an IUnknown ptr which comes through as an object. I've imported the TypeLib and have gotten the interface MfcComLibLib.IA but when I cast I get a failure because of the interface not being implemented.

Is there a way to query the IUnknown pointer to discover what interfaces are actually implemented on the object?


Solution

  • Same way as you do with managed interfaces. You use the C# is or as operators. The CLR will under the hood map that to QueryInterface calls.