Managed Aggregation COM

I understand that creating a COM object that aggregates an existing COM object involves implementing the redirection logic in the IUnknown.QueryInterface method of the external object.

I have a question how to do this if the object you are building is being managed. On managed objects, IUnknown is clearly not implemented. COM Interop does it for you. So, how do I tell COM Interop that the object I'm building is an aggregation of another COM object?

So far, the only way to find all the interfaces of the internal object on the external and explicitly redirect them. This a) is ugly and b) assumes that you know all the interfaces to implement, which is not the case in my situation.

Any thoughts?

+6
aggregation com
source share
1 answer

If you are using .NET 4, you can use ICustomQueryInterface to override the default IUnknown.QueryInterface logic. There is a sample for COM aggregation on CodePlex - the implementation is quite simple:

 CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out IntPtr ppv) { if(iid.Equals(new Guid("00000000-0000-0000-0000-000000001234"))) { ppv = Marshal.GetComInterfaceForObject(this.innerObject, typeof(IInnerInterface), CustomQueryInterfaceMode.Ignore); return CustomQueryInterfaceResult.Handled; } ppv = IntPtr.Zero; return CustomQueryInterfaceResult.NotHandled; } 
+7
source share

All Articles