Handling a VB6 event in C# - why does it only work sometimes?

2

I have a VB6 application implemented as an ActiveX exe. I also have a C# application that interacts with the VB6 app via COM.

This all works fine except in one scenario.

If the VB6 app is kicked off from the C# app, everything is fine. If, however, the VB6 app is already running stand-alone, then although the COM interface still works, the C# event handlers never fire.

A (very simplified) extract of the code follows, names & GUIDs changed to protect the innocent.

VB6 app: myVB6App.cls (GlobalSingleUse)

Event myEvent()

public function RaiseMyEvent()
    RaiseEvent myEvent
end function

(partial) IDL generated from the built VB6 exe:

...
[
  uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),
  version(1.10),
  appobject
]
coclass myApp {
    [default] interface _myApp;
    [default, source] dispinterface __myApp;
};
...
[
  uuid(yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy),
  version(1.10),
  hidden,
  nonextensible
]
dispinterface __myApp {
    properties:
    methods:
        ...
        [id(0x00000005)]
        void myEvent();
        ...
};
...

C# app:

public class myAppInterface : IDisposable, ImyAppEvents
{
    public delegate void myEventDelegate();
    public event myEventDelegate myEventHandler;
    private object _myApp = null;
    private IConnectionPoint _connectionPoint;
    private int _sinkCookie;

    public myAppInterface()
    {
        _myApp = Activator.CreateInstance(Type.GetTypeFromProgID("myVB6ProjectName.myApp"));
        Guid g = new Guid("yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy");
        connectionPointContainer.FindConnectionPoint(ref g, out _connectionPoint);
        _connectionPoint.Advise(this, out _sinkCookie);
    }
    public void myEvent()
    {
        if (myEventHandler != null)
        {
            myEventHandler();
        }
    }
}

[ComImport, Guid("yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"), TypeLibType((short)4240)]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface ImyAppEvents
{
    [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(5)]
        void myEvent();
}

public class myC#App
{
    private static myAppInterface _vb6App;
    public static myAppInterface VB6Application
    {
        get
        {
            if (_vb6App == null)
            {
                _vb6App = new myAppInterface();
                _vb6App.myEventHandler += new myAppInterface.myEventDelegate(DoSomething);
            }
            return _vb6App;
        }
    }
    static void DoSomething()
    {
        //code to actually handle the event
    }
}

As I say, if at the point that Activator.CreateInstance runs, the VB6 exe is not currently running, everything work as expected and the code in DoSomething() is executed when myEvent is fired in the VB6 app.

If the VB6 app is running stand-alone beforehand, the C# app can still control it via COM (methods not shown above for clarity) but the DoSomething() code never runs in response to myEvent.

Any ideas where I'm going wrong?

c#
vb6
interop
event-handling
events
asked on Stack Overflow Oct 16, 2009 by PD.

2 Answers

4

I would think that the problem lies in setting the sink for the event. At a quick guess I would say that you are creating a new instance of the VB component and assigning a new sink to the events generated by that instance rather than to the component already running.

The VB6 documentation states:

Setting the Instancing property of a class to GlobalMultiUse or GlobalSingleUse allows client programs to use the properties and methods of the class as if they were global functions, but within the project where you defined the GlobalMultiUse class module, objects created from the class are not global.

The implication is that when you try to connect to a running instance, you're actually getting a new copy of the VB6 class, with it's own events, and NOT the already running class. Hence not receiving the events.

answered on Stack Overflow Oct 16, 2009 by ChrisBD • edited Jan 25, 2021 by StayOnTarget
3

I agree with ChrisBD, I think you are creating a new instance of the VB component. Have you tried GlobalMultiUse rather than GlobalSingleUse? The VB6 documentation says:

SingleUse allows other applications to create objects from the class, but every object of this class that a client creates starts a new instance of your component.

I would also set the project thread properties to thread pool with only one thread, to stop multiple threads being created within the VB6 exe. It works for me in a similar situation.

answered on Stack Overflow Oct 16, 2009 by MarkJ • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0