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

  • SEARCH
  • Home
  • 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 292549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:11:25+00:00 2026-05-12T06:11:25+00:00

I am upgrading vb.net app that handles the events of a COM object (possibly

  • 0

I am upgrading vb.net app that handles the events of a COM object (possibly written in VB6) from framework 1.1 to WPF 2.0/3.5

The code: (object names simplified for brevity)

public class MyClass
   Private WithEvents serviceMonitor As COMOBJECT.ServiceMonitor

   Public Sub New()
       serviceMonitor = New COMOBJECT.ServiceMonitor()
       serviceMonitor.Connect([some ip address])
   End Sub

   Private Sub ServiceMonitor_ServiceConnectionUp(ByVal MonitorId As Integer, ByVal UserArg As Integer) _ 
        Handles serviceMonitor.ServiceConnectionUp

        Debug.WriteLine("connection up!")
   End Sub

    ' other similar handlers omitted
End Class

The app will get the callbacks as expected, however within a few seconds I get an Access Violation. The basic callback code is similar to the .net 1.1 version although it ran perfectly.

According to my research on the error, it is caused by the garbage collector moving things around. Since I am not passing the DLL any objects to manipulate, I am guessing that the callbacks are the issues. Other folks have solved this via a delegate with <UnmanagedFunctionPointer(CallingConvention.Cdecl)> and/or Marshal.GetFunctionPointerForDelegate.

Unfortunately, all the examples I have found are cases where the DLL has some sort of SetCallback(IntPtr) method. I am using WithEvents and the Handles keyword. Here is my attempt (note that I removed the Handles keyword so that I can use AddHandler:

<UnmanagedFunctionPointer(CallingConvention.Cdecl)> _
   Delegate Sub ServiceMonitor_ServiceConnectionUpDelegate(ByVal MonitorId As Integer, ByVal UserArg As Integer)

public class MyClass
   Private WithEvents serviceMonitor As COMOBJECT.ServiceMonitor

   Public Sub New()
       serviceMonitor = New COMOBJECT.ServiceMonitor()
       del = New ServiceMonitor_ServiceConnectionUpDelegate(AddressOf ServiceMonitor_ServiceConnectionUp)
       AddHandler serviceMonitor.ServiceConnectionUp, del ' <--- Error here
       serviceMonitor.Connect([some ip address])
   End Sub

   Private Sub ServiceMonitor_ServiceConnectionUp(ByVal MonitorId As Integer, ByVal UserArg As Integer) 

        Debug.WriteLine("connection up!")
    End Sub

    ' other similar handlers omitted
End Class

The error I am getting on the AddHandler line is: “Value of type MyClass.ServiceMonitor_ServiceConnectionUpDelegate cannot be converted to COMOBJECT._IServiceMonitorEvents_ServiceConnectionUpEventHandler“

When I mouse hover the mentioned Event Handler it has a signature of:
Delegate Sub _IServiceMonitorEvents_ServiceConnectionUpEventHandler(ByVal MonitorId As Integer, ByVal UserArg As Integer)

The signatures are identical so I’m not sure what the problem is.

Question 1: How do I use a delegate with AddHandler in this fashion?
Question 2: Does Marshal.GetFunctionPointerForDelegate() need to be involved? It returns an IntPtr but AddHandler wants a delegate.

Thanks in advance.

  • 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-12T06:11:25+00:00Added an answer on May 12, 2026 at 6:11 am

    Although I am not yet 100% certain that the issue is fixed, so far I haven’t had any more access violations.

    Basically I removed the WithEvents keyword to prevent VB from using its own COM -> .net error handling and instead used the methods in the library intended for C++ clients. I used the <UnmanagedFunctionPointer(CallingConvention.Cdecl)> and passed the library a Marshal.GetFunctionPointerForDelegate IntPtr. I also kept a reference to the IntPtr althought it may have been overkill.

    I am confused that I had to go through all this since the WithEvents handlers worked perfectly in .net 1.1. The main differences was that I used a form to handle the callbacks rather than a class.

    One lingering issue is the fact that the debugger sometimes stays running when I close the apps form. I am hoping it is just a debugger quirk.

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

Sidebar

Related Questions

Upgrading .Net Events system from Tridion 2009 to Tridion 2011 SP1. Does anyone have
We are just upgrading our ASP.Net shop implementation (from simple one) to structure that
I'm upgrading an ASP.NET 4.0 app from: Windows Server 2003 and IIS 6 to:
I am upgrading an application that was originally written in VS 2005 (.NET 3.0)
i'm upgrading my asp.net mvc app to the MVC 3 from mvc 2. I
I am upgrading a web app from ASP.NET 3 Preview 1 to the RTM
I have got a question with regards to upgrading asp.net mvc applications from v1
I tried upgrading an ASP.Net application from Visual Studio 2005 to 2008, and I
I am a .Net developer that has been tasked with upgrading a classic asp
I have started upgrading one of our internal software applications, written in ASP.NET Web

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.