如何查找动态加载的 com 对象的成员

发布于 2024-12-08 00:27:50 字数 347 浏览 1 评论 0原文

我正在尝试使用没有任何文档的 OLE COM 对象。我通过以下方式动态加载com对象:

dynamic comObj = Activator.CreateInstance(Type.GetTypeFromProgID("The Program ID"));

注册表中的程序ID指向某个exe(或者至少我是这么认为的,我不能确定。有没有办法知道它到底指向哪里)。我尝试在 OLE COM 查看器中加载 exe,但无法从中获取任何有用的信息。例如,我无法找到一种我确信存在的方法。实际上,加载 comObject 后,我​​可以毫无问题地调用此方法,但我想知道是否有一种方法可以列出/查看此 COM 对象的所有成员。

I am trying to use an OLE COM object that I don't have any documentation for. I load the com object dynamically by:

dynamic comObj = Activator.CreateInstance(Type.GetTypeFromProgID("The Program ID"));

The Program ID in the registry points to some exe (or at least that is what I think, I cannot tell for sure. Is there a way to know where exactly does it point to). I tried loading the exe in an OLE COM viewer, but I was not able to get any useful information out of it. For example, I was not able to find a method that I knew for sure was there. Actually, after loading the comObject I am able to invoke this method without any problems, but I would like to know if there is a way that I can list/view all the members of this COM object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

本宫微胖 2024-12-15 00:27:50

给定 ProgID,您可以使用 API 函数和/或查找注册表(例如在 HKEY_CLASSES_ROOT 键下)来获取 COM 服务器 CLSID 以及托管类,包括可执行文件的完整路径。

如果您在那里有对类型库的引用,您还可以加载它并检查已实现的接口和接口信息。您也可以从 COM 对象的实例获取此信息,前提是它实现了 IDispatch、IDispatchEx、IprovideClassInfo 等接口。

Given ProgID you can use API functions and/or look up registry (such as under HKEY_CLASSES_ROOT key) for COM server CLSID and binary that hosts the class, including full path to executable.

If you have a reference to a type library there, you can also load it and check implemented interfaced and interface information. You can also possibly obtain this information from an instance of COM object too, provided that it implements interfaces like IDispatch, IDispatchEx, IProvideClassInfo.

待天淡蓝洁白时 2024-12-15 00:27:50

您可以通过 IDispatch 接口枚举所有方法(如果它支持)。

这是一篇使用 IDispatch 获取成员信息的 MSDN 文章

You can enumerate all of the methods through the IDispatch interface, if it supports it.

Here is an MSDN article that uses IDispatch to get member info

请别遗忘我 2024-12-15 00:27:50

我不记得我在哪里复制了这个来源。恳请作者原谅。

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Runtime.InteropServices.CustomMarshalers;

namespace ConsoleApplication1
{
    [
       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);
    }

    class Program
    {
        static void Main(string[] args)
        {
            Type t1 = Type.GetTypeFromProgID("WbemScripting.SWbemDateTime");
            Object o1 = Activator.CreateInstance(t1);

            IDispatch disp2 = o1 as IDispatch;
            if (disp2 != null)
            {
                Type t3;
                disp2.GetTypeInfo(0, 0, out t3);

                MemberInfo[] mlist3 = t3.GetMembers();
            }
        }
    }
}

您可以在 C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\ 中找到 CustomMarshalers.dll

I don't remember where I copied this source. I beg a pardon from the author.

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Runtime.InteropServices.CustomMarshalers;

namespace ConsoleApplication1
{
    [
       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);
    }

    class Program
    {
        static void Main(string[] args)
        {
            Type t1 = Type.GetTypeFromProgID("WbemScripting.SWbemDateTime");
            Object o1 = Activator.CreateInstance(t1);

            IDispatch disp2 = o1 as IDispatch;
            if (disp2 != null)
            {
                Type t3;
                disp2.GetTypeInfo(0, 0, out t3);

                MemberInfo[] mlist3 = t3.GetMembers();
            }
        }
    }
}

You can find the CustomMarshalers.dll in C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文