当实例是动态的时,获取事件并不容易.但正如您发现的那样,您可以使用原始的COM接口(IConnectionPoint、IConnectionPointContainer和IDispatch)获取它们
下面是一个C#实用程序类,它包装IDispatch
并连接到所请求的"调度接口"事件接口.
首先要做的是确定:
- 您想要的Display接口的IID(接口ID)(包含所需事件的IID).
- 事件DISPID(标识事件的整数).
为此,您可以使用OleView tool from the Windows SDK,打开描述COM对象支持的公共接口的类型库文件.它通常是一个.TLB文件(或嵌入在.dll中),但对于Office,它是一个.OLB.对于Word,它位于C:\Program Files\Microsoft Office\root\Office16\MSWORD.OLB
(或类似的路径).
在此示例中,我希望获得Application.DocumentOpen个事件.这是OleView向我展示的:
因此,以下是如何获得活动的方法:
static void Main()
{
var comTypeName = "Word.Application";
var comType = Type.GetTypeFromProgID(comTypeName);
dynamic comObj = Activator.CreateInstance(comType);
try
{
// to get IID and DISPIDs from DispInterfaces, open C:\Program Files\Microsoft Office\root\Office16\MSWORD.OLB (or similar)
// with OleView tool from Windows SDK
var dispatcher = new Dispatcher(new Guid("000209FE-0000-0000-C000-000000000046"), comObj);
dispatcher.Event += (s, e) =>
{
switch (e.DispId)
{
case 4: // method DocumentOpen(Document doc)
dynamic doc = e.Arguments[0]; // arg 1 is "doc"
Console.WriteLine("Document '" + doc.Name + "' opened.");
break;
}
};
comObj.Documents.Open(@"c:\somepath\some.docx");
}
finally
{
comObj.Quit(false);
}
}
和Dispatcher实用程序类:
using System;
using System.Runtime.InteropServices;
using System.Threading;
public class Dispatcher : IDisposable, Dispatcher.IDispatch, ICustomQueryInterface
{
private IConnectionPoint _connection;
private int _cookie;
private bool _disposedValue;
public event EventHandler<DispatcherEventArgs> Event;
public Dispatcher(Guid interfaceId, object container)
{
ArgumentNullException.ThrowIfNull(container);
if (container is not IConnectionPointContainer cpContainer)
throw new ArgumentException(null, nameof(container));
InterfaceId = interfaceId;
Marshal.ThrowExceptionForHR(cpContainer.FindConnectionPoint(InterfaceId, out _connection));
_connection.Advise(this, out _cookie);
}
public Guid InterfaceId { get; }
protected virtual void OnEvent(object sender, DispatcherEventArgs e) => Event?.Invoke(this, e);
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
var connection = Interlocked.Exchange(ref _connection, null);
if (connection != null)
{
connection.Unadvise(_cookie);
_cookie = 0;
Marshal.ReleaseComObject(connection);
}
_disposedValue = true;
}
}
~Dispatcher() { Dispose(disposing: false); }
public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); }
CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out IntPtr ppv)
{
if (iid == typeof(IDispatch).GUID || iid == InterfaceId)
{
ppv = Marshal.GetComInterfaceForObject(this, typeof(IDispatch), CustomQueryInterfaceMode.Ignore);
return CustomQueryInterfaceResult.Handled;
}
ppv = IntPtr.Zero;
if (iid == IID_IManagedObject)
return CustomQueryInterfaceResult.Failed;
return CustomQueryInterfaceResult.NotHandled;
}
int IDispatch.Invoke(int dispIdMember, Guid riid, int lcid, System.Runtime.InteropServices.ComTypes.INVOKEKIND wFlags, ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams, IntPtr pvarResult, IntPtr pExcepInfo, IntPtr puArgErr)
{
var args = pDispParams.cArgs > 0 ? Marshal.GetObjectsForNativeVariants(pDispParams.rgvarg, pDispParams.cArgs) : null;
var evt = new DispatcherEventArgs(dispIdMember, args);
OnEvent(this, evt);
var result = evt.Result;
if (pvarResult != IntPtr.Zero)
{
Marshal.GetNativeVariantForObject(result, pvarResult);
}
return 0;
}
int IDispatch.GetIDsOfNames(Guid riid, string[] names, int cNames, int lcid, int[] rgDispId) => E_NOTIMPL;
int IDispatch.GetTypeInfo(int iTInfo, int lcid, out /*ITypeInfo*/ IntPtr ppTInfo) { ppTInfo = IntPtr.Zero; return E_NOTIMPL; }
int IDispatch.GetTypeInfoCount(out int pctinfo) { pctinfo = 0; return 0; }
private const int E_NOTIMPL = unchecked((int)0x80004001);
private static readonly Guid IID_IManagedObject = new("{C3FCC19E-A970-11D2-8B5A-00A0C9B7C9C4}");
[ComImport, Guid("00020400-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IDispatch
{
[PreserveSig]
int GetTypeInfoCount(out int pctinfo);
[PreserveSig]
int GetTypeInfo(int iTInfo, int lcid, out /*ITypeInfo*/ IntPtr ppTInfo);
[PreserveSig]
int GetIDsOfNames([MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)] string[] names, int cNames, int lcid, [Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] int[] rgDispId);
[PreserveSig]
int Invoke(int dispIdMember, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, int lcid, System.Runtime.InteropServices.ComTypes.INVOKEKIND wFlags, ref System.Runtime.InteropServices.ComTypes.DISPPARAMS pDispParams, IntPtr pvarResult, IntPtr pExcepInfo, IntPtr puArgErr);
}
[ComImport, Guid("b196b286-bab4-101a-b69c-00aa00341d07"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IConnectionPoint
{
[PreserveSig]
int GetConnectionInterface(out Guid pIID);
[PreserveSig]
int GetConnectionPointContainer(out IConnectionPointContainer ppCPC);
[PreserveSig]
int Advise([MarshalAs(UnmanagedType.IUnknown)] object pUnkSink, out int pdwCookie);
[PreserveSig]
int Unadvise(int dwCookie);
[PreserveSig]
int EnumConnections(out /*IEnumConnections**/ IntPtr ppEnum);
}
[ComImport, Guid("b196b284-bab4-101a-b69c-00aa00341d07"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface IConnectionPointContainer
{
[PreserveSig]
int EnumConnectionPoints(out /*IEnumConnectionPoints*/ IntPtr ppEnum);
[PreserveSig]
int FindConnectionPoint([MarshalAs(UnmanagedType.LPStruct)] Guid riid, out IConnectionPoint ppCP);
}
}
public class DispatcherEventArgs : EventArgs
{
public DispatcherEventArgs(int dispId, params object[] arguments)
{
DispId = dispId;
Arguments = arguments ?? Array.Empty<object>();
}
public int DispId { get; }
public object[] Arguments { get; }
public object Result { get; set; }
}