Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to expose a C# Enum to COM Interop callers, and if so, how?

I have a managed assembly that gets called via COM Interop. Like a VBScript client, a Perl client, and so on.

The classes are decorated with

[ClassInterface(ClassInterfaceType.AutoDual)]
[GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")]
[ComVisible(true)]

Then of course I do the regasm thing, and all the methods work just fine.

But there are also enum types in the assembly. I'd like to use symbolic names COM applications, for the enum values.

How do I expose the enums via COM interop? Do I just need to add these attributes?

[GuidAttribute("ebc25cf6-9120-4283-b972-0e5520d0000E")]
[ComVisible(true)]

And then, how do I reference those symbolic names in VBScript? I don't see the enum types in OleView. (Should I?) I see all the other types in OleView.

like image 746
Cheeso Avatar asked Aug 14 '09 07:08

Cheeso


1 Answers

My (so far only) .NET assembly that I made COM-visible also had an enum type, which showed up just fine in OleView. I had the whole library be COM-visible so

[ComVisible(true)]

was not necessary. Is your enum type public?

One thing that happened was that the different enumerations were 'prefixed' with 'enum type name'_:

public enum DataType
{
    INT32,
    FLOAT64,
    INT8
}

turned into:

typedef [...]
enum {
    DataType_INT32 = 0,
    DataType_FLOAT64 = 1,
    DataType_INT8 = 2
} DataType;

in the type library.

like image 99
Andreas F Avatar answered Oct 06 '22 08:10

Andreas F