COMException in C# when hooking into event

守給你的承諾、 提交于 2019-12-07 03:02:32

问题


I am receiving a COM Exception when trying to hook into an event on a COM Object. Here is the code I am trying to execute.

COMClass a = IComClass as ComClass;
a.SomeEvent += new SomeEvent_EventHandler(MethodNameHere);

Line two throws an exception of type COMException with the following information:

System.Runtime.InteropServices.COMException was caught

Message="Exception from HRESULT: 0x80040202"

Source="mscorlib"

ErrorCode=-2147220990

StackTrace: at System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(Object pUnkSink, Int32& pdwCookie)

Does anyone have any ideas why I am unable to hook into a COM event or if there is a workaround for hooking into COM events?

Chris


回答1:


The problem was the interface for the events was no registered. Once I added the registry key for the Events Interface, this resolved the problem. I was able to get the interface id information using OLEViewer.exe




回答2:


The error code you got is CONNECT_E_CANNOTCONNECT, something that Googles well. It indicates that the COM server isn't happy about your attempt to subscribe an event handler. Why it is not is something you'll need to find out. Getting help from the component author or vendor is almost always required.

One thing you can try is to look at the type library with Oleview.exe and find out if the event you're trying to subscribe to is on a dispinterface that's marked as the default source interface. If it is not, try casting the object to the dispinterface type, then subscribe to its event.



来源:https://stackoverflow.com/questions/2246525/comexception-in-c-sharp-when-hooking-into-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!