使用动态转储 ComObject 对象?

发布于 2024-12-27 11:21:26 字数 138 浏览 4 评论 0原文

我正在尝试(不幸的是)为我在 Office 类型库中访问的对象实现“对象转储器”。

肯定有可能,因为 VS 的调试窗口有一个 System.__ComObject 对象的“动态视图”,它可以有效地完成我想要的操作。

有什么想法吗?

I'm trying (without luck) to implement an "Object Dumper" for objects I'm accessing in the Office Type Library.

It must be possibly, because VS's debug window has a "dynamic view" for the System.__ComObject objects that effectively does what I want.

Any ideas?

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

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

发布评论

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

评论(1

冷了相思 2025-01-03 11:21:26

我还创建了一个方法来获取可用于访问对象的接口。使用:

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

您的代码中必须有一个 IDispatch 接口:

[Guid("00020400-0000-0000-C000-000000000046"), ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDispatch 
{ 
    [PreserveSig] 
    int GetTypeInfoCount(out int pctinfo); 

    [PreserveSig] 
    int GetTypeInfo( 
        [MarshalAs(UnmanagedType.U4)] int iTInfo, 
        [MarshalAs(UnmanagedType.U4)] int lcid, 
        out System.Runtime.InteropServices.ComTypes.ITypeInfo ppTInfo); 

    [PreserveSig] 
    int GetIDsOfNames( 
        ref Guid riid, 
        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)] 
        string[] rgszNames, 
        int cNames, 
        int lcid, 
        [MarshalAs(UnmanagedType.LPArray)] int[] rgDispId); 

    [PreserveSig] 
    int Invoke( 
        int dispIdMember, 
        ref Guid riid, 
        uint lcid, 
        ushort wFlags, 
        ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams, 
        out object pVarResult, 
        ref System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo, 
        IntPtr[] puArgErr); 
}

我的方法将 COM 对象转换为 IDispatch,获取非托管类型信息,从非公共 Marshal 方法 GetTypeInfoGuid 获取对象类型 GUID,然后在当前 AppDomain 的程序集中搜索它。

internal static Func<ITypeInfo,Guid> GetTypeInfoGuid = (Func<ITypeInfo,Guid>)Delegate.CreateDelegate(typeof(Func<ITypeInfo,Guid>), typeof(Marshal).GetMethod("GetTypeInfoGuid", BindingFlags.NonPublic | BindingFlags.Static, null, new[]{typeof(ITypeInfo)}, null), true);
public static Type GetCOMObjectType(object comobject)
{
    if(!Marshal.IsComObject(comobject))
    {
        throw new ArgumentException("This is not COM object.", "comobject");
    }
    IDispatch dispatch = (IDispatch)comobject;
    ITypeInfo info;
    dispatch.GetTypeInfo(0,0, out info);
    Guid guid = GetTypeInfoGuid(info);
    foreach(Assembly a in AppDomain.CurrentDomain.GetAssemblies())
    {
        foreach(Type t in a.GetTypes())
        {
            if(t.IsInterface && t.IsImport && t.GUID == guid)
            {
                return t;
            }
        }
    }
    return Type.GetTypeFromCLSID(guid);
}

如果未找到适当的类型,该方法将返回 System.__ComObject 类型。但是,您可以从中获取 GUID 属性值,因此至少您将获得 GUID。
用法:

object controls = axWindowsMediaPlayer1.cdromCollection;
Type t = GetCOMObjectType(controls);
Console.WriteLine(t);
Console.WriteLine(t.GUID);
/*Output:
  WMPLib.IWMPCdromCollection
  ee4c8fe2-34b2-11d3-a3bf-006097c9b344 */

希望这会有所帮助。祝你好运。 ☺

编辑:
找到了一个更简单但速度较慢的方法:

object controls = axWindowsMediaPlayer1.cdromCollection;
IDispatch dispatch = (IDispatch)controls;
ITypeInfo info;
dispatch.GetTypeInfo(0,0, out info);
Type t = Marshal.GetTypeForITypeInfo(Marshal.GetIUnknownForObject(info));
Console.WriteLine(t);

I have also created a method for getting an interface that can be used for accessing the object. Use:

using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;

You have to have an IDispatch interface in your code:

[Guid("00020400-0000-0000-C000-000000000046"), ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IDispatch 
{ 
    [PreserveSig] 
    int GetTypeInfoCount(out int pctinfo); 

    [PreserveSig] 
    int GetTypeInfo( 
        [MarshalAs(UnmanagedType.U4)] int iTInfo, 
        [MarshalAs(UnmanagedType.U4)] int lcid, 
        out System.Runtime.InteropServices.ComTypes.ITypeInfo ppTInfo); 

    [PreserveSig] 
    int GetIDsOfNames( 
        ref Guid riid, 
        [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr)] 
        string[] rgszNames, 
        int cNames, 
        int lcid, 
        [MarshalAs(UnmanagedType.LPArray)] int[] rgDispId); 

    [PreserveSig] 
    int Invoke( 
        int dispIdMember, 
        ref Guid riid, 
        uint lcid, 
        ushort wFlags, 
        ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams, 
        out object pVarResult, 
        ref System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo, 
        IntPtr[] puArgErr); 
}

My method casts COM object to IDispatch, gains unmanaged type info, gets object type GUID from non-public Marshal method GetTypeInfoGuid and then searchs it in current AppDomain's assemblies.

internal static Func<ITypeInfo,Guid> GetTypeInfoGuid = (Func<ITypeInfo,Guid>)Delegate.CreateDelegate(typeof(Func<ITypeInfo,Guid>), typeof(Marshal).GetMethod("GetTypeInfoGuid", BindingFlags.NonPublic | BindingFlags.Static, null, new[]{typeof(ITypeInfo)}, null), true);
public static Type GetCOMObjectType(object comobject)
{
    if(!Marshal.IsComObject(comobject))
    {
        throw new ArgumentException("This is not COM object.", "comobject");
    }
    IDispatch dispatch = (IDispatch)comobject;
    ITypeInfo info;
    dispatch.GetTypeInfo(0,0, out info);
    Guid guid = GetTypeInfoGuid(info);
    foreach(Assembly a in AppDomain.CurrentDomain.GetAssemblies())
    {
        foreach(Type t in a.GetTypes())
        {
            if(t.IsInterface && t.IsImport && t.GUID == guid)
            {
                return t;
            }
        }
    }
    return Type.GetTypeFromCLSID(guid);
}

If the appropriate type isn't found, the method will return type of System.__ComObject. However, you can get GUID property value from it, so at least you will get the GUID.
Usage:

object controls = axWindowsMediaPlayer1.cdromCollection;
Type t = GetCOMObjectType(controls);
Console.WriteLine(t);
Console.WriteLine(t.GUID);
/*Output:
  WMPLib.IWMPCdromCollection
  ee4c8fe2-34b2-11d3-a3bf-006097c9b344 */

Hope this helps. Good luck. ☺

Edit:
Found an easier but slower method:

object controls = axWindowsMediaPlayer1.cdromCollection;
IDispatch dispatch = (IDispatch)controls;
ITypeInfo info;
dispatch.GetTypeInfo(0,0, out info);
Type t = Marshal.GetTypeForITypeInfo(Marshal.GetIUnknownForObject(info));
Console.WriteLine(t);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文