托管COM聚合
这是我的理解构建一个COM对象聚合一个现有的COM对象意味着在外部对象的IUnknown.QueryInterface方法中实现重定向逻辑。
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.
是如何做到这一点,如果你正在建设的对象被管理。对于托管对象IUnknown没有显式实现COM Interop为您做。那么如何告诉COM Interop,我构建的对象是另一个COM对象的聚合?
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?
到目前为止,我发现的唯一方法是实现所有的接口内部对象在外部并明确地重定向它们。这是一个丑陋和b)假设你知道所有的实现接口,这是不是在我的情况。
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.
任何想法?
如果你使用的是.NET 4,那么你可以使用 ICustomQueryInterface 覆盖默认 IUnknown.QueryInterface
逻辑。
CodePlex上有一个 COM聚合示例 - 实现非常简单:
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;
}