0

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_PARTmacros in place in my class as well.

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

1 Answer 1

2

MyEventHandler's GUID has to equal sourceGuid AND the current assembly HAS to be COMVisible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.