管理COM聚集COM

2023-09-03 11:28:42 作者:曲终人尽散╮物是已人非

这是我的理解构建聚集现有的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互操作为您完成。所以,我该怎么办告诉COM互操作,我建立的对象是另一个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 逻辑。 有对COM聚集一个样品在codePLEX - 实施是很简单

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;
}