I am developing a COM dll library, and I have a little vb.net (vs 2005) application just to test it.
I had my object declared in the application as
Private m_VarName As MyLib.CMyComClass
So far, so good.
But now, I need an event to inform the application of some things, so I implemented such event in the COM dll, and changed the declaration to
Private WithEvents m_VarName As MyLib.CMyComClass
So far, so good, again. But if I add a Sub to handle my event:
Private Sub m_VarName_OnCaptureStop() Handles m_VarName.MyEvent
...
End Sub
The first time I create the object, nothing bad happens, but if I reinstantiate it
If (Not m_VarName Is Nothing) Then ReleaseComObject(m_VarName)
m_VarName= New MyLib.CMyComClass
then I get a cryptic TargetInvocationException, seemingly related to reflection (which, AFAIK, I am not using).
If I remove the "Handles m_VarName.MyEvent" part, everything seems to work. In case it matters, I am not firing any event, for now. Any idea about why this happens?