Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / April 2004

Tip: Looking for answers? Try searching our database.

Multiple WithEvents references causes crash

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Trevor F. - 15 Apr 2004 05:30 GMT
I am having problems getting events to to work right using
COM Interop. I am developing a class library in C# which
is exposed to COM and called by a VBA application. Several
of the classes in the class library raise events and i'm
having no problem recieving these events in my VBA code
(using WithEvents), however as soon as i try to hold more
than one reference to the same object i start getting
crashes when i try and release the references to the
object. It appears that the order that the references are
released is important (This only happens when using
WithEvents). To try and figure out what is going on I have
created a minimal class library and corresponding vba code
which illustrates the problem. (See below)

Explanation:

- NetClass just exposes a single event 'MyEvent' to COM.
(am i doing this wrong?)
- The VBA class defines two variables using WithEvents
which are set to point to the same instance of NetClass
- Trying to set NetObject1 to Nothing without first
setting NetObject2 to Nothing causes a crash. (wtf?)

Please help, this is driving me nuts!

CODE SNIPPET #1 - The .Net class sourcing events:
-----8<----------
using System;
using System.Runtime.InteropServices;

namespace ComInteropEventsTest
{

   [ComVisible(false)]
   public delegate void MyEventEventHandler();

   [Guid("B83B48F0-8F58-4021-AF55-30AC4F72DC6A")]
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   public interface INetClass_EventSource
   {
       [DispId(1)] void MyEvent();
   }

   [Guid("0871D7F5-DFE2-49a0-9FF6-E61C9A1F2187")]
   public interface INetClass
   {
       [DispId(1)] void RaiseEvent();
   }

   [Guid("B3AD4529-A5EB-4acf-BC33-74E332C50A46")]
   [ClassInterface(ClassInterfaceType.None)]
   [ComSourceInterfaces(typeof(INetClass_EventSource))]
   public class NetClass: INetClass
   {
       public event MyEventEventHandler MyEvent;

       public void RaiseEvent()
       {
           if (null!=MyEvent) MyEvent();
       }
   }

}
-----8<----------

CODE SNIPPET #2 - The VBA class that uses it:
-----8<----------
Option Explicit

Private WithEvents NetObject1 As NetClass
Private WithEvents NetObject2 As NetClass

Public Sub DoStuff()
   
   Set NetObject1 = New NetClass
   Set NetObject2 = NetObject1
   Set NetObject1 = Nothing ' Crash!
       
End Sub
-----8<----------
Trevor F. - 15 Apr 2004 07:41 GMT
I just answered my own question (it is a confirmed bug).
For anyone who is interested have a look at Microsoft
Knowledge Base Article - 827418 'BUG: A Visual Basic
Application Stops Responding While It Disconnects from the
Managed Event Source'. There is a fix available from MS.

http://support.microsoft.com/default.aspx?scid=kb;en-
us;827418

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.