开发者

How to find the members of a dynamically loaded com object

开发者 https://www.devze.com 2023-04-11 13:53 出处:网络
I am trying to use an OLE COM object that I don\'t have any documentation for. I load the com o开发者_运维问答bject dynamically by:

I am trying to use an OLE COM object that I don't have any documentation for. I load the com o开发者_运维问答bject 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.


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.


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


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\

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号