Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Managed COM aggregation

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?

like image 582
mfeingold Avatar asked Dec 30 '09 16:12

mfeingold


1 Answers

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;
}
like image 159
Pent Ploompuu Avatar answered Nov 17 '22 00:11

Pent Ploompuu