如何在 C# .NET 7 运行时为未知类型的 COM 对象后期绑定事件接收器

问题描述 投票:0回答:1

我正在 C# .NET 7 中工作,我可以在其中创建编译时未知类型的 COM 对象。

var comTypeName = "Word.Application";//Assume this is passed in by the user and is unknown at compile time.
var comType = Type.GetTypeFromProgID(comTypeName);
var comObj = Activator.CreateInstance(comType);

我希望收到 COM 对象上发生的事件的通知。我对

IConnectionPoint/IConnectionPointContainer
、事件接收器、
IConnectionPoint.Advise()
进行了广泛的研究,但我发现没有任何东西可以解决我的问题。所以我怀疑这个问题要么在 C# 中不可行,要么是如此明显和公理化,以至于没有人觉得有必要在任何文档中解释它。我希望是后者。

问题的关键是我发现的每个示例都适用于提前知道类型的 COM 对象。因此,代码知道要监听哪些事件,并定义一个实现这些事件的接口,并将其传递给

IConnectionPoint.Advise()
:

icpt = (IConnectionPoint)someobject;
icpt.Advise(someinterfacethatimplementsallevents, out var _cookie);

根据我的研究,

Advise()
的第一个参数是一个实现源对象正在寻找的接口的对象。那么,当我在编译时甚至不知道源对象是什么时,我怎么知道该接口应该是什么?

一些研究似乎说接收器对象应该实现

IDispatch
。但是会调用什么方法呢?
Invoke()

这听起来有些合理,但 .NET Core 之前的任何内容(我使用的是 .NET 7)都已经剥离了

IDispatch
和许多其他 COM 功能。所以他们说我们应该使用 .NET 中不再存在的接口?

为了开始解决这个问题,我从各种在线来源中发现了

IDispatch
的实现:

[Guid("00020400-0000-0000-c000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[ComImport]
public interface IDispatch
{
    //
    // Omitting type info functions for brevity.
    //
    
    //Invoke seems to be what we care about.
    void Invoke(int dispIdMember,
        [MarshalAs(UnmanagedType.LPStruct)] Guid iid,
        int lcid,
        System.Runtime.InteropServices.ComTypes.INVOKEKIND wFlags,
        [In, Out][MarshalAs(UnmanagedType.LPArray)]
        System.Runtime.InteropServices.ComTypes.DISPPARAMS[] paramArray,
        out object? pVarResult,
        out System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo,
        out uint puArgErr);
}

我不确定的是我是否应该做这样的事情:

public class MyDispatch : IDispatch
{
    void Invoke(int dispIdMember,
        [MarshalAs(UnmanagedType.LPStruct)] Guid iid,
        int lcid,
        System.Runtime.InteropServices.ComTypes.INVOKEKIND wFlags,
        [In, Out][MarshalAs(UnmanagedType.LPArray)]
        System.Runtime.InteropServices.ComTypes.DISPPARAMS[] paramArray,
        out object? pVarResult,
        out System.Runtime.InteropServices.ComTypes.EXCEPINFO pExcepInfo,
        out uint puArgErr)
        {
            //Do something with the event info and dispatch to the appropriate place in my code.
            //What I *really* need here is the string name of the event so that I can figure out where to properly dispatch it to.
            /*
            if (nameofevent == "Changed")
                ChangeHandler();
            else if (nameofevent == "Closed")
                ClosedHandler();
            */
        }
}

至此,我已经查完了解决此问题的在线可用信息,但不确定如何进一步进行。

c# events com com-interop
1个回答
0
投票

当实例是动态的时候,获取事件并不容易。但正如您所发现的,您可以使用原始 COM 接口(IConnectionPointIConnectionPointContainerIDispatch)来获取它们

这是一个 C# 实用程序类,它包装

IDispatch
并连接到请求的“dispinterface”事件接口。

首先要做的是确定:

  • 您要查找的调度接口的 IID(接口 ID)(包含您需要的事件的接口)。
  • 事件 DISPID(标识事件的整数)。
为此,您可以使用 Windows SDK 中的 OleView 工具,打开描述 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); } }

以及调度程序实用程序类:
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; }
}


© www.soinside.com 2019 - 2024. All rights reserved.