Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1058809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:04:44+00:00 2026-05-16T18:04:44+00:00

I am developing a COM dll library, and I have a little vb.net (vs

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:04:45+00:00Added an answer on May 16, 2026 at 6:04 pm

    The reason why has to do with the implementation of WithEvents and Handles in VB.Net. When you declare a field with the WithEvents modifier in VB.net it will be generated as a property.

    Whenever that property is updated via an assignment operation the property setter will unsubscribe from the old event handler and then subscribe to the event handler on the new value. In effect it looks like this

    Property m_VarName as SomeType 
      Set   
        if _m_VarName isNot Nothing Then
          RemoveHandler _m_VarName.SomeEVent,m_VarName_OnCaptureStop
        End If
        _m_VarName = Value
        AddHandler _m_VarName.SomeEvent,m_VarName_OnCaptureStop
      End Set 
    End Property
    

    If you consider that in the context of your code what’s essentially happening is the following

    ReleaseComObject(m_VarName)
    RemoveHandler m_VarName.SomeEvent,m_VarName_OnCaptureStop
    

    So you’re calling RemoveHandler on a COM object which is already released. Hence it throws an Exception.

    The fix is simple, don’t call ReleaseComObject. The number of situations where you actually need to call this method are very small and almost certainly not applicable to this situation. Instead just let the GC collect it normally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.