イベントを発生させるように設定されたアンマネージ C++ COM サーバーがあり、C# アプリからこれらのイベントを処理しようとしています。
ただし、ハンドラーをセットアップするときに InvalidCastException が発生します
myCOMObj.MyCOMEvent += new MyCOMSource_MyCOMEventHandler(handler);
スタック トレースは次を示します。
指定されたキャストは無効です。System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise (オブジェクト 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);
wheremyEventNotifier
は、次のように定義されたクラスのオブジェクトです。
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class EventNotifier : MyCOMSource
...
connectionPoint.Advise
しかし、スタックトレースで同じ InvalidCastException を取得します
指定されたキャストは無効です。System.Runtime.InteropServices.ComTypes.IConnectionPoint.Advise (オブジェクト pUnkSink、Int32 & pdwCookie) で
私はこれがクライアント側の問題であると想定しています。なぜなら、私が独自の ConnectionPoint を実行しようとしたときと、フレームワークにそれを実行させたときの動作が一貫しているためです。しかし、それがサーバー側の何かである場合:
COMサーバー側では、このように宣言しました
coclass MyCOMCoClass
{
[default] dispinterface MyCOMInterface;
[default, source] dispinterface MyCOMSource;
};
クラスにもCONNECTION_MAP
とCONNECTION_PART
マクロが配置されています。
どうすればこれをデバッグできますか?