Groups | Blog | Home
all groups > dotnet interop > august 2007 >

dotnet interop : VB6 doesn't release events in .NET component


Sascha Fueller
8/27/2007 12:00:00 AM
Hello,

I have a class in C# with events, which is exposed as a COM-object.
When I have a VB6 Client that is using this class, I see that eventhandlers
are not removed, when VB releases the com object.

I'm using VS2005, but it seems that the behaviour is the same with VS2003

Is there something wrong with the way I'm using this, or may it be that this
is an error with NET or VB?
It doesn't seem to be related to the garbage collector, because when I call
GC.Collect(), remove doesn't get called either.

Thanks in advance
Sascha


Here is my code

C#:
Register for COM interop has to be enabled

[quoted text, click to view]
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace NetComLib
{
// delegate definition
[ComVisible(false)]
public delegate void ChannelChangedDelegate(int channel);

// Event-Interface
[ComVisible(true)]
[Guid("30BBBC2F-03E0-497c-82BF-93B1D775B794")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface INetComLibEvents
{
void ChannelChanged(int channel);
}

// Interface
[ComVisible(true)]
[Guid("0EB15C71-DF7B-46e3-8686-2A7708D9487A")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface INetComLib
{
void SetChannel(int channelNo);
}

// COM-object
[ComVisible(true)]
[ProgId("Test.NetComLib")]
[Guid("C2D2CE9B-09A6-475e-A839-C8F90C630F43")]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces("NetComLib.INetComLibEvents")]
public class NetComLib : INetComLib
{
public void SetChannel(int channelNo)
{
if (channelChanged != null)
channelChanged(channelNo);
}

private event ChannelChangedDelegate channelChanged;

public event ChannelChangedDelegate ChannelChanged
{
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
add
{
this.channelChanged += value;
Trace.WriteLine("Eventhandler added");
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
remove
{
this.channelChanged -= value;
Trace.WriteLine("Eventhandler removed");
}
}
}
}
<< C# End <<<<<<<<<<<<<<<<<

And here is the code of my VB-Form with one button

<< VB Begin <<<<<<<<<<<<<<<
Dim WithEvents comLib As NetComLib.NetComLib
Dim i As Integer


Private Sub comLib_ChannelChanged(ByVal channel As Long)
MsgBox channel
End Sub

Private Sub Command1_Click()
If comLib Is Nothing Then
Set comLib = New NetComLib.NetComLib
comLib.SetChannel i
i = i + 1
Else
Set comLib = Nothing
End If
End Sub
<< VB End <<<<<<<<<<<<<<<<<

Sascha Fueller
9/5/2007 1:46:03 PM
Hello,

after some further searching I was able to find the following
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=774207&SiteID=1

it seems that this proble is fixed in .NET Framework 2.0 SP1

Best regards
Sascha

"Sascha Fueller" <sascha.fueller@b*deletethis*oschrexroth.de> schrieb im
Newsbeitrag news:fatu57$pi$1@loghost1.eu.boschrexroth.com...
[quoted text, click to view]

AddThis Social Bookmark Button