6

我正在编写一些测试代码来模拟调用我的后期绑定 COM 对象的 c# 实现的非托管代码。我有一个声明为 IDispatch 类型的接口,如下所示。

 [Guid("2D570F11-4BD8-40e7-BF14-38772063AAF0")]
 [InterfaceType(ComInterfaceType.InterfaceIsDual)]
 public interface TestInterface
 {
     int Test();
 }

 [ClassInterface(ClassInterfaceType.AutoDual)]
 public class TestImpl : TestInterface 
 {
 ...
 }

当我使用下面的代码调用 IDispatch 的GetIDsOfNames函数时

  ..
  //code provided by Hans Passant
  Object so = Activator.CreateInstance(Type.GetTypeFromProgID("ProgID.Test"));
  string[] rgsNames = new string[1];
  int[] rgDispId = new int[1];
  rgsNames[0] = "Test";

  //the next line throws an exception
  IDispatch disp = (IDispatch)so;

其中 IDispatch 定义为:

 //code provided by Hans Passant
 [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("00020400-0000-0000-C000-000000000046")]
 private interface IDispatch {
     int GetTypeInfoCount();
     [return: MarshalAs(UnmanagedType.Interface)]
     ITypeInfo GetTypeInfo([In, MarshalAs(UnmanagedType.U4)] int iTInfo, [In, MarshalAs(UnmanagedType.U4)] int lcid);
     void GetIDsOfNames([In] ref Guid riid, [In, MarshalAs(UnmanagedType.LPArray)] string[] rgszNames, [In, MarshalAs(UnmanagedType.U4)] int cNames, [In, MarshalAs(UnmanagedType.U4)] int lcid, [Out, MarshalAs(UnmanagedType.LPArray)] int[] rgDispId);
  }

抛出 InvalidCastException。是否可以将 ac# 接口转换为 IDispatch?

4

2 回答 2

1

您应该能够对 COM 类型使用反射来获取方法列表。

Type comType = Type.GetTypeFromProgID("ProgID.Test");
MethodInfo[] methods = comType.GetMethods();
于 2011-11-09T18:41:05.993 回答
1

您需要使用 regasm 注册您的程序集,并且需要使用 [ComVisible] 属性标记要从 COM 访问的类。您可能还需要使用 tlbexp(生成)和 tregsvr 来生成和注册类型库来注册它。

此外(从 Win32 的角度来看)“disp = (IDispatch) obj”与“disp = obj as IDispatch”不同 - 使用 'as' 运算符实际上调用对象上的 QueryInterface 方法以获取指向所请求接口的指针,而不是尝试将对象强制转换为接口。

最后,使用 c# 的“动态”类型可能更接近其他人访问您的课程的做法。

于 2012-03-13T01:27:00.190 回答