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

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

问题描述

我通过 COM 连接到某个程序并接收 System.__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 实用程序.如果存在,则类型库通常嵌入在 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 转换工作,很可能还有一个类型库可用.COM 服务器作者创建一个非常简单.另一个可以用来查看类型库的工具是 OleView.exe,View + Typelib.

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 命名空间中可用.您可以使用 Reflector 从框架代码中挖掘 IDispatch 声明.

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.

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

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