HighTechTalks DotNet Forums  

VB6 doesn't release events in .NET component

Dotnet Framework (Interop) microsoft.public.dotnet.framework.interop


Discuss VB6 doesn't release events in .NET component in the Dotnet Framework (Interop) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Sascha Fueller
 
Posts: n/a

Default VB6 doesn't release events in .NET component - 08-27-2007 , 03:23 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

Quote:
C# Begin
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.InterfaceIsIDispatc h)]
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.Synchro nized)]
add
{
this.channelChanged += value;
Trace.WriteLine("Eventhandler added");
}
[System.Runtime.CompilerServices.MethodImpl(System. Runtime.CompilerServices.MethodImplOptions.Synchro nized)]
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 <<<<<<<<<<<<<<<<<




Reply With Quote
  #2  
Old   
Sascha Fueller
 
Posts: n/a

Default Re: VB6 doesn't release events in .NET component - 09-05-2007 , 07:46 AM






Hello,

after some further searching I was able to find the following
http://forums.microsoft.com/MSDN/Sho...74207&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 (AT) loghost1 (DOT) eu.boschrexroth.com...
Quote:
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

C# Begin
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.InterfaceIsIDispatc h)]
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.Synchro nized)]
add
{
this.channelChanged += value;
Trace.WriteLine("Eventhandler added");
}

[System.Runtime.CompilerServices.MethodImpl(System. Runtime.CompilerServices.MethodImplOptions.Synchro nized)]
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




Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.