[英]InvalidCastException when setting up handler for COM event
我有一個非托管的C ++ COM服務器,該服務器設置為觸發事件,並且我正在嘗試從C#應用程序處理這些事件。
但是,設置處理程序時,我收到InvalidCastException
myCOMObj.MyCOMEvent += new MyCOMSource_MyCOMEventHandler(handler);
堆棧跟蹤顯示:
指定的演員表無效。 在System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(Object pUnkSink,Int32&dwCookie)在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
上得到了相同的InvalidCastException與堆棧跟蹤
指定的演員表無效。 在System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise(對象pUnkSink,Int32&pdwCookie)
我假設這是客戶端的問題,因為當我嘗試做自己的ConnnectionPoint東西時,以及當我讓框架為我做這件事時,行為始終如一。 但是如果是服務器端的話:
在COM服務器端,我已經這樣聲明了
coclass MyCOMCoClass
{
[default] dispinterface MyCOMInterface;
[default, source] dispinterface MyCOMSource;
};
我的班上也有CONNECTION_MAP
和CONNECTION_PART
宏。
怎么回事,我該如何調試?
MyEventHandler的GUID必須等於sourceGuid,並且當前程序集必須是COMVisible。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.