我正在尝试(没有运气)为我在Office Type Library中访问的对象实现“Object Dumper”。
必定可能,因为VS的调试窗口具有System .__ ComObject对象的“动态视图”,可以有效地完成我想要的操作。
有什么想法吗?
答案 0 :(得分:3)
我还创建了一个获取可用于访问对象的接口的方法。使用:
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);