C# COM互操作:如何从LPDISPATCH获取CCW?

C# COM互操作:如何从LPDISPATCH获取CCW?,c#,com,interop,winfax,C#,Com,Interop,Winfax,我正在用C编写一个应用程序,它通过IDispatch连接到一个旧的skool COM对象。我使用这种代码来实现这一点: public sealed class Attachments { Object comObject; Type type; private readonly static Attachments _instance = new Attachments(); public static Attachments Instance { get {

我正在用C编写一个应用程序,它通过IDispatch连接到一个旧的skool COM对象。我使用这种代码来实现这一点:

public sealed class Attachments
{
    Object comObject;
    Type type;

    private readonly static Attachments _instance = new Attachments();
    public static Attachments Instance  { get { return _instance; } }

    private Attachments()
    {
        type = Type.GetTypeFromProgID("WinFax.Attachments");
        if (type == null)
            throw new ArgumentException("WinFax Pro is not installed.");
        comObject = Activator.CreateInstance(type);
    }

    public Int16 Count()
    {
        Int16 x = (Int16) type.InvokeMember("Count",
                                            BindingFlags.InvokeMethod,
                                            null,
                                            comObject,
                                            null);
        return x;
    }
    ....
这个IDispatch接口上的一个方法返回一个LPDISPATCH,我认为它是指向IDispatch的长指针。它是另一个COM对象ProgId WinFax.Attachment。附件管理WinFax.Attachments对象的集合

在C中,如何调用与该LPDISPATCH对应的COM对象上的方法?我可以这样做吗:

    Object o = type.InvokeMember("MethodReturnsLpdispatch",
                                     BindingFlags.InvokeMethod,
                                     null,
                                     comObject,
                                     null);
    Type t2 = Type.GetTypeFromProgID("WinFax.Attachment"); // different ProgId !!
    Object x = t2.InvokeMember("MethodOnSecondComObject",  
                                     BindingFlags.InvokeMethod,
                                     null,
                                     o,
                                     null);
是的,这是有效的:

    Type type = Type.GetTypeFromProgID("WinFax.Attachments");
    if (type == null)
          throw new ArgumentException("WinFax Pro is not installed.");
    Object comObject = Activator.CreateInstance(type);  
    Object o2 = type.InvokeMember("MethodReturnsLpdispatch",
                                     BindingFlags.InvokeMethod,
                                     null,
                                     comObject,
                                     null);
    Type t2 = Type.GetTypeFromProgID("WinFax.Attachment"); // different ProgId !!
    Object x = t2.InvokeMember("MethodOnSecondComObject",  
                                     BindingFlags.InvokeMethod,
                                     null,
                                     o2,
                                     null);

使用o.GetType。乞讨、借用或偷窃在此处使用动态关键字。或者在VB.NET中编写适配器。