views:

1412

answers:

3

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  A: 

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.

RS Conley
Thank you for the post and for the link. Hmm, I actually think I do have that in the above. I had the properties in reverse order, but I think everything is there. I don't have the Guid set though -- I'll try that out, but I'd read elsewhere that it wasn't necessary (or, rather, would be generated automatically)
Eric
+1  A: 

Your post did lead me to the solution however -- thank you!

Not surprisingly, it turned out to be a typo.

/* 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();
}

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

Again, thank you for the help!

Eric
A: 

I try to make an msn program on Vb6, But I get "Object or Class does not support the set of events " error.I use Wmlive 9, But old messenger I have. What is wrong?Can I use old messenger library's with wmlive 9?

Moonstar