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's GUID has to equal sourceGuid AND the current assembly HAS to be COMVisible.