麻烦铸造COM对象的C#/。NET类 [英] Trouble Casting COM Object to a C#/.NET Class

查看:126
本文介绍了麻烦铸造COM对象的C#/。NET类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个是用C#实现在IDL库中定义一个接口的基类。我有一个管理这个类的一个实例C ++类。我也有一个需要使用这个类的[实例(也就是通过C ++项目获得)C#项目。我想投我的基类实现回我的C#基类中的COM接口。

I have a base class that is written in C# that implements an interface defined in an IDL library. I have a C++ class that manages the single instance of this class. I also have a C# project that needs to make use of this class [instance (that is obtained through the C++ project)]. I want to cast the COM interface that my base class implements back into my C# base class.

然而,当我投,我从C获得的通用对象++比重为我的C#的基类,我收到一个错误。当我投它派生的接口,是没有问题的。

However, when I cast the generic object that I acquired from the C++ proportion into my C# base class, I received an error. When I cast it to the derived interface, there is no problem.

下面是我的问题类似的例子。如果作用域在CSDriver的NETBaseInstance函数不计算这两个。该NETBASE是实现COMInterface唯一的类。所以,我知道肯定值是一个NETBASE,因为如果范围COMInterface评价。

Below is the similar example of my problem. The two if scopes are never evaluated in the NETBaseInstance function of CSDriver. The NETBase is the only class that implements COMInterface. So, I know for sure that the value is a NETBase since the if scope for COMInterface is evaluated.

public class NETBase : COMInterface{ ... }

public class CSDriver{
    private NETBase m_NETBaseInstance;

    ...

    public object NETBaseInstance{
        set{
            COMInterface test;
                if( value is COMInterface ){
                // This is evaluated.
                test = value as COMInterface;
            }

            if( value is NETBase ){
                // This is not evaluated.
                m_NETBaseInstance = value as NETBase;
            }

            if( test is NETBase ){
                // This is not evaluated.
                m_NETBaseInstance = test as NETBase;
            }
        }
    }
}

什么是过程,这个COM对象转换为成功的C#的对象吗?难道我做错了什么?

What is the process to cast this COM object to the C# object successfully? Am I doing something wrong?

感谢您事先的任何答复。

Thank you in advance for any answers.

推荐答案

有没有办法,你可以直接投它。

There is no way you can cast it directly.

在非托管C ++ API函数返回一个指针 COMInterface 接口虚表;你可能会认为你知道NETBASE是唯一实现这个接口,但是.NET COM互操作的基础设施有没有这样的知识(事实上,严格来说你就错了,因为实施 COMInterface 其中非托管code是处理是由互操作的基础设施,而不是NETBASE的实例)创建一个COM可调用包装;当你调用从C#的C ++ API,所有的.NET互操作层可以做的是通过你的管理codeA运行时可调用包装周围的非托管COM接口,这RCW不是 NETBASE <实例/ code>,所以你不能将它转换为 NETBASE ,因为你已经找到。

The unmanaged C++ API is returning a pointer to the COMInterface interface vtable; you might think you know that NETBase is the only implementation of this interface, but the .NET COM Interop infrastructure has no such knowledge (indeed, strictly speaking you are wrong, because the implementation of COMInterface which the unmanaged code is dealing with is a COM Callable Wrapper created by the interop infrastructure, not an instance of NETBase); when you call the C++ API from C#, all the .NET interop layer can do is pass your managed code a Runtime Callable Wrapper around the unmanaged COM interface, and this RCW is not an instance of NETBase, so you cannot cast it to NETBase, as you have found.

有可能是解决方法做你想做的,利用你的专业知识,任何 COMInterface 必须指向一个特定的 NETBASE 实例。细节将取决于 NETBASE 的性质(例如是其状态序列化),并在所有被管理的code不论是否在同一个AppDomain中运行(例如,如果是,您可以提供静态字段和方法上的NETBASE类,允许集合点与该实例的 COMInterface 点 - 平凡的,如果它真的是一个单身的你的问题建议)。

There may be workarounds to do what you want, making use of your special knowledge that any COMInterface must point to a specific NETBase instance. The details will depend on the nature of NETBase (e.g. is its state serializable) and on whether or not all the managed code runs in the same AppDomain (e.g. if yes, you may be able to provide static fields and methods on the NETBase class to allow rendezvous with the instance the COMInterface points to - trivial if it really is a singleton as your question suggests).

这篇关于麻烦铸造COM对象的C#/。NET类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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