5

It is my understanding building a COM object aggregating an existing COM object implies implementing redirection logic in the IUnknown.QueryInterface method of the outer object.

The question I have is how to do that if the object you are building is managed. On managed objects IUnknown is not explicitly implemented COM Interop does it for you. So how do I tell COM Interop that the object I build is an aggregation of another COM object?

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

Any thoughts?

1 Answer 1

7

If you are using .NET 4 then you could use ICustomQueryInterface to override the default IUnknown.QueryInterface logic. There's a sample for COM aggregation on CodePlex - the implementation is quite straightforward:

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;
}
1
  • How to implement the required com interface correctly? The GetComInterfaceForObject does not fail, but IRibbonExtensibility.GetCustomUI never gets called. My question: stackoverflow.com/questions/15921925/…
    – user1261537
    Apr 10, 2013 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.