为 COM 事件设置处理程序时出现 InvalidCastException
我有一个非托管 C++ COM 服务器,它设置为触发事件,并且我正在尝试从我的 C# 应用程序处理这些事件。
但是,在设置处理程序时出现 InvalidCastException
myCOMObj.MyCOMEvent += new MyCOMSource_MyCOMEventHandler(handler);
堆栈跟踪显示:
指定的演员阵容无效。在 System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(对象 pUnkSink, Int32& dwCookie) at MyCOMSource_EventProvider.add_MyCOMEvent(MyCOMSource_MyCOMEventHandler) 在 MyCOMSource_Event.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
...
但我在 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
宏。
可能发生了什么事,我该如何调试?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MyEventHandler 的 GUID 必须等于 sourceGuid 并且当前程序集必须是 COMVisible。
MyEventHandler's GUID has to equal sourceGuid AND the current assembly HAS to be COMVisible.