简体   繁体   中英

InvalidCastException when setting up handler for COM event

I have an unmanaged C++ COM server that is setup to fire events, and i am trying to handle these events from my C# app.

However, I get an InvalidCastException when setting up the handler

myCOMObj.MyCOMEvent += new MyCOMSource_MyCOMEventHandler(handler);

The stack trace shows:

Specified cast is not valid. at System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(Object pUnkSink, Int32& dwCookie) at MyCOMSource_EventProvider.add_MyCOMEvent(MyCOMSource_MyCOMEventHandler) at MyCOMSource_Event.add_MyCOMEvent(MyCOMSource_MyCOMEventHandler)

I have tried setting up my own IConnectionPoint like this

IConnectionPointContainer connectionPointContainer = (IConnectionPointContainer)myCOMObj;
Guid sourceGuid = typeof(MyCOMSource).GUID;
IConnectionPoint connectionPoint;
connectionPointContainer.FindConnectionPoint(ref sourceGuid, out connectionPoint);
int cookie;
connectionPoint.Advise(myEventNotifier, out cookie);

where myEventNotifier is an object of class defined like this:

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class EventNotifier : MyCOMSource
...

But i get the same InvalidCastException at connectionPoint.Advise with the stack trace

Specified cast is not valid. at System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(Object pUnkSink, Int32& pdwCookie)

I'm assuming this is an issue on the client side because of the consistent behavior when i try to do my own ConnnectionPoint stuff and when i let the framework do it for me. But in case it's something on the server side:

On the COM server side i have declared it like this

coclass MyCOMCoClass
{
    [default] dispinterface MyCOMInterface;
    [default, source] dispinterface MyCOMSource;
};

I have the CONNECTION_MAP and CONNECTION_PART macros in place in my class as well.

What could be going on, how can i debug this?

MyEventHandler的GUID必须等于sourceGuid,并且当前程序集必须是COMVisible。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM