我有一个非托管的C++ COM服务器被设置为触发事件,并且我试图从我的C#应用程序中处理这些事件。当为COM事件设置处理程序时出现InvalidCastException
然而,建立处理程序时,我得到一个InvalidCastException
myCOMObj.MyCOMEvent += new MyCOMSource_MyCOMEventHandler(handler);
堆栈跟踪显示:
指定的转换是无效的。在 System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise在 MyCOMSource_Event.add_MyCOMEvent(MyCOMSource_MyCOMEventHandler)
(对象pUnkSink,的Int32 & dwCookie)在 MyCOMSource_EventProvider.add_MyCOMEvent(MyCOMSource_MyCOMEventHandler)我试图建立我自己的的IConnectionPoint这样
IConnectionPointContainer connectionPointContainer = (IConnectionPointContainer)myCOMObj;
Guid sourceGuid = typeof(MyCOMSource).GUID;
IConnectionPoint connectionPoint;
connectionPointContainer.FindConnectionPoint(ref sourceGuid, out connectionPoint);
int cookie;
connectionPoint.Advise(myEventNotifier, out cookie);
其中myEventNotifier
是这样定义的类的对象:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class EventNotifier : MyCOMSource
...
,但我得到同样的InvalidCastException的在connectionPoint.Advise
与堆栈跟踪
指定的转换无效。在System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(对象pUnkSink,的Int32 & pdwCookie)
我假设这是因为一致的行为在客户端的问题,当我试图做我自己ConnnectionPoint的东西,当我让框架为我做。但如果它的东西在服务器端:在我的课
在COM服务器端我已经宣布它像这样
coclass MyCOMCoClass
{
[default] dispinterface MyCOMInterface;
[default, source] dispinterface MyCOMSource;
};
我在地方CONNECTION_MAP
和CONNECTION_PART
宏为好。
怎么回事,我该如何调试?