0

IDispatchを介して古いスクールのCOMオブジェクトに接続するアプリをC#で作成しています。私はこの種のコードでこれを行います:

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インターフェイスのメソッドの1つは、LPDISPATCHを返します。これは、IDispatchへのロングポインターです。これは別のCOMオブジェクト、ProgIdWinFax.Attachmentです。(WinFax.Attachmentsは、WinFax.Attachmentオブジェクトのコレクションを管理します。)

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);
4

1 に答える 1

0

はい、これは機能します:

    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);
于 2011-02-03T17:33:35.687 に答える