如何枚举COM对象的成员在C#中? [英] How to enumerate members of COM object in C#?

查看:183
本文介绍了如何枚举COM对象的成员在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我连接到通过COM的一些程序和接收系统.__ ComObject。我知道这几种方法,所以我可以做这样的:

I connect to some program via COM and receive System.__ComObject. I know several methods of it, so I can do like this:

object result = obj.GetType().InvokeMember("SomeMethod", BindingFlags.InvokeMethod, null, obj, new object[] { "Some string" });

和这样

dynamic dyn = obj;
dyn.SomeMethod("Some string");

这两种方法都工作正常。但我怎么能确定COM对象的内部类型信息,并通过列举其所有成员?

Both methods works fine. But how can I determine inner type information of com object and enumerate through all its members?

我尝试这样做:

[ComImport, Guid("00020400-0000-0000-C000-000000000046"),
  InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IDispatch
{
  void Reserved();
  [PreserveSig]
  int GetTypeInfo(uint nInfo, int lcid, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(TypeToTypeInfoMarshaler))] out System.Type typeInfo);
}

...

IDispatch disp = (IDispatch)obj;
Type t;
disp.GetTypeInfo(0, 0, out t);

但T是在结束零。谁能帮我?

But the t is null at the end. Can anyone help me?

推荐答案

您不能得到一个类型的COM对象。这就需要在创建的COM组件的互操作库。这当然是如果COM服务器类型库的低痛点,只需添加对它的引用或运行Tlbimp.exe将实用程序。如果是present那么该类型库通常嵌入在DLL中。当你明白了,无论是编辑和对象浏览器获得了很多有关COM类可用的方法和属性更聪明。

You cannot get a Type for the COM object. That would require you creating an interop library for the COM component. Which is certainly the low pain point if the COM server has a type library, just add a reference to it or run the Tlbimp.exe utility. If it is present then the type library is usually embedded inside the DLL. When you got that, both the editor and the Object Browser get a lot smarter about the method and properties available on the COM class.

眼看IDispatch的科协工作使得它很可能是一个类型库是可用。这是pretty的平凡的COM服务器的作者来创建一个。你可以用它来偷看类型库的另一个工具是OLEView.exe这类,查看+类型库。

Seeing the IDispatch cast work makes it quite likely that a type library is available as well. It is pretty trivial for the COM server author to create one. Another tool you can use to peek at the type library is OleView.exe, View + Typelib.

如果还是不行,那么你的确可以挖掘的东西出来的IDispatch。你的声明看起来腥,为的IDispatch :: GetTypeInfo的第三个参数是的ITypeInfo,一个COM接口。无需自定义编组,ITypeInfo的是在System.Runtime.InteropServices.ComTypes命名空间中可用。你可以挖了IDispatch声明出来的框架code与反射。

If that doesn't work then you can indeed dig stuff out of IDispatch. Your declaration looks fishy, the 3rd argument for IDispatch::GetTypeInfo is ITypeInfo, a COM interface. No need for a custom marshaller, ITypeInfo is available in the System.Runtime.InteropServices.ComTypes namespace. You can dig the IDispatch declaration out of the framework code with Reflector.

和,有不可替代的体面的文件。你应该能够得到一些时,你有一个许可使用该组件。

And of course, there's no substitute for decent documentation. You should be able to get some when you got a license to use this component.

这篇关于如何枚举COM对象的成员在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆