为 COM 事件设置处理程序时出现 InvalidCastException

发布于 2024-09-26 17:19:15 字数 1625 浏览 3 评论 0原文

我有一个非托管 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_MAPCONNECTION_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_PARTmacros in place in my class as well.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-10-03 17:19:15

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文