在Excel处于编辑模式时引发事件时,C#COM服务器事件"丢失"

Jas*_*onF 9 c# com excel vba excel-vba

我有一个用C#编写的进程内COM服务器(使用.NET Framework 3.5)基于此示例引发COM事件:http: //msdn.microsoft.com/en-us/library/dd8bf0x3(v = vs.90 )的.aspx

Excel VBA是我的COM服务器中最常见的客户端.我发现当我在Excel处于编辑模式(例如正在编辑单元格)时引发COM事件时,事件"丢失".这意味着,永远不会调用VBA事件处理程序(即使在Excel编辑模式完成之后),并且对C#事件委托的调用也会通过并静默失败,不会抛出任何异常.有谁知道如何在我的COM服务器上检测到这种情况?或者更好的是仍然使事件委托调用块,直到Excel退出编辑模式?

我试过了:

  • 检查事件委托的属性 - 找不到任何属性来指示无法在客户端上引发事件.
  • 直接从工作线程和主线程调用事件委托 - 在客户端上未引发事件,服务器上不会抛出异常.
  • 将事件委托推送到工作线程的Dispatcher并同步调用它 - 在客户端上没有引发事件,服务器上没有抛出异常.
  • 将事件委托推送到主线程的Dispatcher并同步和异步调用它 - 在客户端上不引发事件,服务器上没有抛出异常.
  • 检查Dispatcher.BeginInvoke调用的状态代码(使用DispatcherOperation.Status) - 状态始终以"已完成"结束,并且永远不会处于"已中止"状态.
  • 创建一个out-of-proc C#COM服务器exe并测试从那里引发事件 - 相同的结果,事件处理程序从未调用,没有异常.

由于我没有表明客户端没有引发事件,我无法在代码中处理这种情况.

这是一个简单的测试用例.C#COM服务器:

namespace ComServerTest
{
    public delegate void EventOneDelegate();

    // Interface
    [Guid("2B2C1A74-248D-48B0-ACB0-3EE94223BDD3"), Description("ManagerClass interface")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [ComVisible(true)]
    public interface IManagerClass
    {
        [DispId(1), Description("Describes MethodAAA")]
        String MethodAAA(String strValue);

        [DispId(2), Description("Start thread work")]
        String StartThreadWork(String strIn);

        [DispId(3), Description("Stop thread work")]
        String StopThreadWork(String strIn);
    }

    [Guid("596AEB63-33C1-4CFD-8C9F-5BEF17D4C7AC"), Description("Manager events interface")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [ComVisible(true)]
    public interface ManagerEvents
    {
        [DispId(1), Description("Event one")]
        void EventOne();
    }

    [Guid("4D0A42CB-A950-4422-A8F0-3A714EBA3EC7"), Description("ManagerClass implementation")]
    [ComVisible(true), ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces(typeof(ManagerEvents))]
    public class ManagerClass : IManagerClass
    {
        private event EventOneDelegate EventOne;

        private System.Threading.Thread m_workerThread;
        private bool m_doWork;
        private System.Windows.Threading.Dispatcher MainThreadDispatcher = null;

        public ManagerClass()
        {
            // Assumes this is created on the main thread
            MainThreadDispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;

            m_doWork = false;
            m_workerThread = new System.Threading.Thread(DoThreadWork);
        }

        // Simple thread that raises an event every few seconds
        private void DoThreadWork()
        {
            DateTime dtStart = DateTime.Now;
            TimeSpan fiveSecs = new TimeSpan(0, 0, 5);
            while (m_doWork)
            {
                if ((DateTime.Now - dtStart) > fiveSecs)
                {
                    System.Diagnostics.Debug.Print("Raising event...");
                    try
                    {
                        if (EventOne != null)
                        {
                            // Tried calling the event delegate directly
                            EventOne();

                            // Tried synchronously invoking the event delegate from the main thread's dispatcher
                            MainThreadDispatcher.Invoke(EventOne, new object[] { });

                            // Tried asynchronously invoking the event delegate from the main thread's dispatcher
                            System.Windows.Threading.DispatcherOperation dispOp = MainThreadDispatcher.BeginInvoke(EventOne, new object[] { });

                            // Tried synchronously invoking the event delegate from the worker thread's dispatcher.
                            // Asynchronously invoking the event delegate from the worker thread's dispatcher did not work regardless of whether Excel is in edit mode or not.
                            System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(EventOne, new object[] { });
                        }
                    }
                    catch (System.Exception ex)
                    {
                        // No exceptions were thrown when attempting to raise the event when Excel is in edit mode
                        System.Diagnostics.Debug.Print(ex.ToString());
                    }

                    dtStart = DateTime.Now;
                }
            }
        }

        // Method should be called from the main thread
        [ComVisible(true), Description("Implements MethodAAA")]
        public String MethodAAA(String strValue)
        {
            if (EventOne != null)
            {
                try
                {
                    // Tried calling the event delegate directly
                    EventOne();

                    // Tried asynchronously invoking the event delegate from the main thread's dispatcher
                    System.Windows.Threading.DispatcherOperation dispOp = System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(EventOne, new object[] { });

                    // Tried synchronously invoking the event delegate from the main thread's dispatcher
                    System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke(EventOne, new object[] { });
                }
                catch (System.Exception ex)
                {
                    // No exceptions were thrown when attempting to raise the event when Excel is in edit mode
                    System.Diagnostics.Debug.Print(ex.ToString());
                }
                return "";
            }

            return "";
        }

        [ComVisible(true), Description("Start thread work")]
        public String StartThreadWork(String strIn)
        {
            m_doWork = true;
            m_workerThread.Start();
            return "";
        }

        [ComVisible(true), Description("Stop thread work")]
        public String StopThreadWork(String strIn)
        {
            m_doWork = false;
            m_workerThread.Join();
            return "";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我用regasm注册它:

%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\regasm /codebase ComServerTest.dll /tlb:ComServerTest.tlb
Run Code Online (Sandbox Code Playgroud)

Excel VBA客户端代码:

Public WithEvents managerObj As ComServerTest.ManagerClass
Public g_nCounter As Long

Sub TestEventsFromWorkerThread()
    Set managerObj = New ComServerTest.ManagerClass

    Dim dtStart As Date
    dtStart = DateTime.Now

    g_nCounter = 0

    Debug.Print "Start"

    ' Starts the worker thread which will raise the EventOne event every few seconds
    managerObj.StartThreadWork ""

    Do While True
        DoEvents

        ' Loop for 20 secs
        If ((DateTime.Now - dtStart) * 24 * 60 * 60) > 20 Then
            ' Stops the worker thread
            managerObj.StopThreadWork ""
            Exit Do
        End If

    Loop

    Debug.Print "Done"

End Sub

Sub TestEventFromMainThread()
    Set managerObj = New ComServerTest.ManagerClass

    Debug.Print "Start"

    ' This call will raise the EventOne event
    managerObj.MethodAAA ""

    Debug.Print "Done"
End Sub

' EventOne handler
Private Sub managerObj_EventOne()
    Debug.Print "EventOne " & g_nCounter
    g_nCounter = g_nCounter + 1
End Sub
Run Code Online (Sandbox Code Playgroud)

编辑27/11/2014 - 我一直在对此进行更多调查.

对于引发COM事件的C++ MFC自动化服务器,也会发生此问题.如果我在Excel处于编辑模式时从主线程引发COM事件,则永远不会调用事件处理程序.服务器上没有抛出任何错误或异常,类似于我的C#COM服务器. 但是,如果我使用全局接口表将事件接收器接口从主线程编组主线程,然后调用该事件 - 它将在Excel处于编辑模式时阻塞.(我还使用COleMessageFilter来禁用忙碌对话框而没有响应对话框,否则我会收到异常:RPC_E_CANTCALLOUT_INEXTERNALCALL在内部消息过滤器中调用是非法的.)

(如果您想查看MFC自动化代码,请告诉我,为简洁起见,我正在跳过它)

知道了,我试图在我的C#COM服务器上做同样的事情.我可以实例化全局接口表(使用pinvoke.net中的定义)和消息过滤器(使用MSDN中的IOleMessageFilter定义).但是,事件仍然"丢失",并且在Excel处于编辑模式时不会阻止.

以下是我修改C#COM服务器的方法:

namespace ComServerTest
{
    // Global Interface Table definition from pinvoke.net
    [
        ComImport,
        InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
        Guid("00000146-0000-0000-C000-000000000046")
    ]
    interface IGlobalInterfaceTable
    {
        uint RegisterInterfaceInGlobal(
                [MarshalAs(UnmanagedType.IUnknown)] object pUnk,
                [In] ref Guid riid);

        void RevokeInterfaceFromGlobal(uint dwCookie);

        [return: MarshalAs(UnmanagedType.IUnknown)]
        object GetInterfaceFromGlobal(uint dwCookie, [In] ref Guid riid);
    }

    [
        ComImport,
        Guid("00000323-0000-0000-C000-000000000046") // CLSID_StdGlobalInterfaceTable
    ]
    class StdGlobalInterfaceTable /* : IGlobalInterfaceTable */
    {
    }

    public class ManagerClass : IManagerClass
    {
        //...skipped code already mentioned in earlier sample above...
        //...also skipped the message filter code for brevity...
        private Guid IID_IDispatch = new Guid("00020400-0000-0000-C000-000000000046");
        private IGlobalInterfaceTable m_GIT = null;

        public ManagerClass()
        {
            //...skipped code already mentioned in earlier sample above...
            m_GIT = (IGlobalInterfaceTable)new StdGlobalInterfaceTable();
        }

        public void FireEventOne()
        {
            // Using the GIT to marshal the (event?) interface from the main thread back to the main thread (like the MFC Automation server).
            // Should we be marshalling the ManagerEvents interface pointer instead?  How do we get at it?
            uint uCookie = m_GIT.RegisterInterfaceInGlobal(this, ref IID_IDispatch);
            ManagerClass mgr = (ManagerClass)m_GIT.GetInterfaceFromGlobal(uCookie, ref IID_IDispatch);
            mgr.EventOne(); // when Excel is in edit mode, event handler is never called and does not block, event is "lost"
            m_GIT.RevokeInterfaceFromGlobal(uCookie);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望我的C#COM服务器的行为方式与MFC自动化服务器类似.这可能吗?我想我应该在GIT中注册ManagerEvents接口指针,但我不知道如何获取它?我尝试使用Marshal.GetComInterfaceForObject(this,typeof(ManagerEvents))但只抛出异常:System.InvalidCastException:指定的强制转换无效.

Ali*_*eza 3

您应该使用回调方法而不是事件

1-更改接口:

// Interface
[Guid("2B2C1A74-248D-48B0-ACB0-3EE94223BDD3"), Description("ManagerClass interface")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
public interface IManagerClass
{
    [DispId(1), Description("Describes MethodAAA")]
    String MethodAAA(String strValue);

    [DispId(2), Description("Start thread work")]
    String StartThreadWork(String strIn, [MarshalAs(UnmanagedType.FunctionPtr)] ref Action callback);

    [DispId(3), Description("Stop thread work")]
    String StopThreadWork(String strIn);
}
Run Code Online (Sandbox Code Playgroud)

2-添加一个字段来保存回调方法并更改调用者方法:

    [ComVisible(false)]
    Action callBack;

    // Simple thread that raises an event every few seconds
    private void DoThreadWork()
    {
        DateTime dtStart = DateTime.Now;
        TimeSpan fiveSecs = new TimeSpan(0, 0, 5);
        while (m_doWork)
        {
            if ((DateTime.Now - dtStart) > fiveSecs)
            {
                System.Diagnostics.Debug.Print("Raising event...");
                try
                {
                    if (callBack != null)
                        callBack();
                }
                catch (System.Exception ex)
                {
                    // No exceptions were thrown when attempting to raise the event when Excel is in edit mode
                    System.Diagnostics.Debug.Print(ex.ToString());
                }

                dtStart = DateTime.Now;
            }
        }
    }

    [ComVisible(true), Description("Start thread work")]
    public String StartThreadWork(String strIn, [MarshalAs(UnmanagedType.FunctionPtr)] ref Action callback)
    {
        this.callBack = callback;
        m_doWork = true;
        m_workerThread.Start();
        return "";
    }
Run Code Online (Sandbox Code Playgroud)

3-向您的 VBA 添加一个模块(因为 AddressOf 仅适用于模块 SUB)并将此方法放入该模块内

Dim g_nCounter As Integer

Public Sub callback()
    Debug.Print "EventOne " & g_nCounter
    g_nCounter = g_nCounter + 1
End Sub
Run Code Online (Sandbox Code Playgroud)

4- 将这个新创建的 SUB 的地址传递给您的托管方法:

managerObj.StartThreadWork "", AddressOf Module1.callback
Run Code Online (Sandbox Code Playgroud)