Really pulling my hair out with this one...
I have a C# project with an interface defined as:
/* Externally Accessible API */
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISerial
{
[DispId(1)]
bool Startup();
[DispId(2)]
bool Shutdown();
[DispId(3)]
bool UserInput_FloorButton(int floor_number);
[DispId(4)]
bool Initialize();
}
/* Externally Accesssible Event API */
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISerialEvent
{
[DispId(5)]
void DataEvent();
}
[ComSourceInterfaces(typeof(ISerialEvent), typeof(ISerial))]
[ClassInterface(ClassInterfaceType.None)]
public class SerialIface : ISerial
{
public delegate void DataEvent();
public event DataEvent dEvent;
public bool Initialize()
{
//testing the event callback
if (dEvent != null)
{
dEvent();
}
}
...
}
And the VB6 code looks like:
Private WithEvents objSerial As SerialIface
Private Sub objSerial_DataEvent()
'do something happy'
End Sub
Public Sub Class_Initialize()
Set objSerial = New SerialIface '<---this is the line that fails'
Call objSerial.Initialize '<--Initialize would trigger DataEvent, if it got this far'
End Sub
Well, the normal API-type functions appear to be working (if I declare objSerial without the WithEvents keyword), but I can't for the life of me get the "DataEvent" to work. It fails with the "object or class does not support the set of events" message.
I'd originally lumped the two interfaces together, but then C# complained that DataEvent was not defined in the class. The way it is currently, I am able to view all of the APIs and the one event perfectly in the VB6 object browser -- everything looks like it's there... I just can't make it actually work!
I'm sure I'm missing something obvious or doing something stupid -- but I'm new to the whole interop business, so it's just escaping me entirely.
Help!
Your post did lead me to the solution however -- thank you!
Not surprisingly, it turned out to be a typo.
should be
I was defining the interface using the delegate instead of the event
Again, thank you for the help!
Look at this article here.
Specifically it looks like you missing a declaration that looks something like this.
You have this part
But without the second the vtable and typelib of the COM object doesn't have the Event Maps needed to work with VB6 (or other COM Consumers).
You can use the Google search terms "com event" c# and get a bunch of other good results.