托管COM聚合 [英] Managed COM aggregation

查看:145
本文介绍了托管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;
}

这篇关于托管COM聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆