开发者

"Failed to add event handler" when handling an event from COM

I have a COM server, and a Silverlight (OOTB) app that calls it. I recently did a bit of refactoring of the COM server, and I managed to break event handling between the server and the client, and I can't figure out how I broke it. Here's what I have now:

Server

namespace ServerNS
{
    [Guid("8FFF7AAE-B162-42C2-9F70-269D285CF622")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [ComVisible(true)]
    interface IServerEvents
    {
        [DispId(1开发者_JS百科)]
        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);
            }
        }
    }
}

Client

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.
        }
    }
}

Error I'm getting this exception when I try to add the event handler to the AutomationEvent. This worked before my refactoring (which was to add the IServerEvents interface). comObj is a legitimate instance - I can call other COM methods on it - so I can't figure out why it can't seem to find that event; or if it's the "something failed" part of the error, how to figure out what failed.

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)


It turns out that the issue was that the interface was not declared "public" - so the interface needed to look like this:

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


//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);

}

In OnDocumentComplete

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

Add new method to handle click event

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜