views:

232

answers:

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?

+2  A: 

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.

ChrisBD
Even though the VB6 app is GlobalSingleUse?
PD
Marking this as the answer due to the VB6 behaviour noted in my 2nd comment on MarkJ's similar answer.
PD
+2  A: 

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.

MarkJ
D'oh! Sounds like I have the meaning of Multi- and Single-Use switched about in my head. I'll try it MultiUse and see how I go. The VB6 app isn't my baby, though, so there may be a good reason why it's set SingleUse (although I can't think of one).
PD
Those names are **so** confusing. I have to look up the meaning every single time.
MarkJ
If all else fails, RTFM! 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.Gah. The implication is that when I try to connect to a running instance, I'm 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.
PD