2

我正在尝试(没有运气)为我在 Office 类型库中访问的对象实现“对象转储器”。

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

有任何想法吗?

4

1 回答 1

4

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. ☺</p>

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);
于 2012-06-04T15:25:49.877 回答