2011-02-03 7 views
0

私はC#でIDispatch経由で古いスキルのCOMオブジェクトに接続するアプリケーションを作成しています。私はこのコードの一種でこれを行う:このIDispatchインターフェースのメソッドのCOM相互運用機能:LPDISPATCHからCCWを取得するにはどうすればよいですか?

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を返します。もう一つのCOMオブジェクト、ProgId WinFax.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); 
+0

利用o.GetType()私はちょうどこのような何かを行うことができます。 *ここで* dynamic *キーワードを借りたり盗んだりしてください。または、VB.NETでアダプタを作成します。 –

答えて

0

はい、これは動作します:

​​
関連する問題