处理来自COM的事件时“无法添加事件处理程序”

时间:2011-07-06 21:13:45

标签: c# events com

我有一个COM服务器和一个调用它的Silverlight(OOTB)应用程序。我最近做了一些COM服务器的重构,我设法破坏了服务器和客户端之间的事件处理,我无法弄清楚我是如何破坏它的。这就是我现在所拥有的:

服务器

namespace ServerNS
{
    [Guid("8FFF7AAE-B162-42C2-9F70-269D285CF622")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [ComVisible(true)]
    interface IServerEvents
    {
        [DispId(1)]
        void MyEvent(MyContainer container);
    }

    [Guid("D2F3738D-DD23-472F-9D36-4136E1196890")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [ComVisible(true)]
    public interface IServer
    {
        ...
    }

    [Guid("77253016-1A2A-43CC-A360-04519A4F50F5")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComSourceInterfaces(typeof(IServerEvents))]
    [ProgId("Server.MyServer")]
    [ComVisible(true)]
    public class Server : IServer
    {
        public event MyEventHandler MyEvent;

        public void Trigger(MyContainer container)
        {
            if (MyEvent != null)
            {
                MyEvent(container);
            }
        }
    }
}

客户端

namespace ClientNS
{
    public class Client
    {
        public void Init()
        {
            dynamic comObj = AutomationFactory.CreateObject("Server.MyServer");
            AutomationEvent evt = AutomationFactory.GetEvent(comObj, "MyEvent");

            evt.EventRaised += (sender, e) =>
            {
                //Do Stuff
            }; //Exception occurs here.
        }
    }
}

错误 当我尝试将事件处理程序添加到AutomationEvent时,我收到此异常。这在我的重构之前起作用(这是为了添加IServerEvents接口)。 comObj 是一个合法的实例 - 我可以在其上调用其他COM方法 - 所以我无法弄清楚为什么它似乎无法找到该事件;或者如果它是错误的“失败”部分,如何找出失败的原因。

System.Exception was unhandled by user code
  Message=Failed to add event handler. Possible reasons include: the object does not support this or any events, or something failed while adding the event.
  StackTrace:
       at MS.Internal.Error.MarshalXresultAsException(UInt32 hr, COMExceptionBehavior comExceptionBehavior)
       at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
       at MS.Internal.ComAutomation.ComAutomationNative.ConnectEvent(IntPtr nativePeer, String eventName, RaiseComAutomationEventDelegate raiseComAutomationEventDelegate)
       at MS.Internal.ComAutomation.ComAutomationObject.ConnectEvent(String name)
       at System.Runtime.InteropServices.Automation.AutomationEvent.UpdateConnection()
       at System.Runtime.InteropServices.Automation.AutomationEvent.add_EventRaised(EventHandler`1 value)
       at ClientNS.Client.Init()
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.OnRun(Object argument)

2 个答案:

答案 0 :(得分:0)

事实证明,问题是界面未被声明为“公共” - 因此界面需要如下所示:

public interface IServerEvents
{
    [DispId(1)]
    void MyEvent(MyContainer container);
}

答案 1 :(得分:0)

//DHTMLEventHandler.cs 

using System.Runtime.InteropServices;
using mshtml;

namespace BHO
{
    /// 
    /// Generic Event handler for HTML DOM objects.
    /// Handles a basic event object which receives an IHTMLEventObj which
    /// applies to all document events raised.
    /// 

[ComVisible(true)]
public class DHTMLEventHandler
{
    public DHTMLEvent Handler;
    IHTMLDocument2 Document;


    public DHTMLEventHandler(IHTMLDocument2 doc)
    {
        this.Document = doc;
    }
    [DispId(0)]
    public void Call()
    {
        Handler(Document.parentWindow.@event);
    }
}

/// 
/// Generic HTML DOM Event method handler.
/// 
public delegate void DHTMLEvent(IHTMLEventObj e);

}

在OnDocumentComplete中

IHTMLElement htmlElement = document.getElementById("ele");
DHTMLEventHandler Handler = new DHTMLEventHandler((IHTMLDocument2)webBrowser.Document);
Handler.Handler += new DHTMLEvent(Dummy_OnClick);
htmlElement.onclick = Handler;

添加新方法来处理点击事件

void Dummy_OnClick(IHTMLEventObj e){
// DO Action 
}