5
votes

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!

2
Have you tried declaring you object as one of the event source interfaces rather than as the class interface?JP Alioto
I just figured out that if I use an entirely separate class and interface -- one with only events and no implemented APIs, it works. (furthermore, I can call this separate class from the main one in order to send events -- but this is pretty kludgey. Is there any way to have events and regular APIs in the same class or interface?)EMon
JP, thank you for the quick reply. "Private WithEvents objSerial As ISerialEvent" yields "compile error: object does not source automation events"EMon

2 Answers

7
votes

Look at this article here.

Specifically it looks like you missing a declaration that looks something like this.

[Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E"),
    ClassInterface(ClassInterfaceType.None),
    ComSourceInterfaces(typeof(DBCOM_Events))]
    public class DBCOM_Class : DBCOM_Interface
    {

You have this part

// // Events interface Database_COMObjectEvents 
[Guid("47C976E0-C208-4740-AC42-41212D3C34F0"), 
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface DBCOM_Events 
{
}

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.

4
votes

I was defining the interface using the delegate instead of the event:

/* Externally Accesssible Event API */
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISerialEvent
{
    [DispId(5)]
    void DataEvent();
}

should be

/* Externally Accesssible Event API */
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ISerialEvent
{
    [DispId(5)]
    void dEvent();
}