0
votes

Here at work we have an old VB6 application I need to teach new(er) tricks. The first thing I had to do was have it call methods from a .Net COM-visible DLL written in C#. I have that working. Now I need to have it handle incoming progress notification events from that same DLL. Here is the C# code:

namespace NewTricksDLL
{
   [ComVisible(true)]
   [ComSourceInterfaces(typeof(IManagedEventsToCOM))]
   public class NewTricks
   {
        public delegate void NotificationEventHandler(string Message);
        public event NotificationEventHandler NotifyEvent = null;

        public string SomeMethod(string message)
        {
            return Notify(message);
        }

        private string Notify(string message)
        {
            if (NotifyEvent != null)
                NotifyEvent("Notification Event Raised.");
            return message;
        }
   }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    interface IManagedEventsToCOM
    {
        [DispId(1)]
        void NotifyEvent(string Message);
    }
}

And here is how I attempt to use it in a VB6 form

Option Explicit

Public WithEvents newTricks as NewTricksDLL.NewTricks

Private Sub Command1_Click()
    Dim response as String
    Set newTricks = New NewTricksDLL.NewTricks
    AddHandler newTricks.NotifyEvent, AddressOf NotifyEventHandler
    response = newTricks.SomeMethod("Please send an event...")

End Sub

Private Sub NotifyEventHandler()
    'Nothing
End Sub

My problem is that when I attempt to run the VB6 form I get Compile error: Object does not source automation events.

If I remove WithEvents the Command1_Click Sub does run and response does contain "Please send an event..." so I know the method is being called via COM.

Where am I going wrong with the implementation of the event?

2
AddHandler is a vb.net verb, not vb6. If you try to compile this code with vb6, you should receive a compile error "Sub or Function not defined". Unless you have a Sub or Function named AddHandler?MarkL
I have actually been commenting out that line whenever I remove WithEvents. I am sure I would have had trouble with AddHandler once the problem I detailed above is solved.davecove
I expect your C# code isn't defining an event, or maybe it's not included in the COM interface (my C# isn't strong enough to see what's wrong there). That's why you're getting the compile error you report. You'd should also see that if you inspect the newTricks object with the Object Viewer, you won't see an event as part of that interface. Once that's resolved, delete the AddHandler line. Add an event handler by selecting, in the form code window, newTricks from the object selection list, then select the event from the procedure selection list. The event handler will be added to your code.MarkL
@MarkL Curiously, not even SomeMethod() shows up as a member of newTricks in the ObjectBrowser, even tho calls to SomeMethod() complete successfully.davecove
Does this answer your question? Subscribe to C# .net Event in VB6StayOnTarget

2 Answers

1
votes

You declared the object as WithEvents so the object should appear in the left dropdown in the code window in Visual Studio (VB6 IDE). Once you select your object, newTricks in your case, the right dropdown will display the available events. Click on the desired event and the IDE will generate the event handler for you, but you can also type this in manually:

Private Sub newTricks_NotifyEvent()
    ' handle your event here
End Sub
1
votes

The issue turned out to be a lack of proper decoration of the classes and interfaces. This C# code does allow the VB6 IDE to see available events and generate handler templates for the events, but attempting to instantiate the class in VB6 results in Object or class does not support the set of events. I have opened a new question for that issue.

{
    [ComVisible(true)]
    [Guid("16fb3de9-3ffd-4efa-ab9b-0f4117259c75")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ITransfer
    {
        string SendAnEvent();
    }

    [ComVisible(true)]
    [Guid("16fb3de9-3ffd-4efa-ab9b-0f4117259c74")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface IManagedEventsToCOM
    {
        [DispId(1)]
        void NotificationEvent();

    }

    [ComVisible(true)]
    [Guid("dcf177ab-24a7-4145-b7cf-fa06e892ef21")]
    [ComSourceInterfaces(typeof(IManagedEventsToCOM))]
    [ProgId("ADUTransferCS.NewTricks")]
    public class NewTricks : ITransfer
    {
        public delegate void NotificationEventHandler();
        public event NotificationEventHandler NotifificationEvent;

        public string SendAnEvent()
        {
            if (NotifificationEvent != null)
                NotifificationEvent();           
        }
    }
}