2011-02-03 1 views
0

IDispatch를 통해 오래된 skool COM 개체에 연결하는 C#에서 응용 프로그램을 작성하고 있습니다. 나는 코드의 종류와이를 수행이 IDispatch 인터페이스에 메소드의COM interop : 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(). Beginner, * 빌려주거나 * dynamic * 키워드를 여기에서 사용하도록 훔칩니다. 또는 VB.NET에서 어댑터를 작성하십시오. –

답변

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); 
관련 문제