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?