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

10,896

Solution 1

VBScript and other late-bound clients use IDispatch to call methods on objects. As such, these languages don't have access to type information in the typelib -- they just create an object from a GUID, get an IDispatch pointer back, and start calling methods by name.

I'm unsure of the COM interop part of the question, but even if the enums did show in OleView, you wouldn't be able to use them directly.

However, if you are able to publish the enums in the typelib, I wrote a tool eons ago that can generate a script file (vbs or js) containing all the enums from a typelib as constants.

See here: http://www.kontrollbehov.com/tools/tlb2const/

Solution 2

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.

Share:
10,896

Related videos on Youtube

Cheeso
Author by

Cheeso

I'm a longtime hacker and software practitioner. Creator of IIRF, DotNetZip, ReloadIt, CleanModQueue and a bunch of other Apigee-related tools and scripts. Maintainer of csharp-mode and a few other emacs-y things . I'm a Polyglot: Java, JavaScript, golang, a little PHP, C#, some VB.NET, C, XSLT, Powershell, elisp of course. Currently most of the work I do is JavaScript and NodeJS. I work for Google. We're hiring API geeks around the world. see: https://careers.google.com/jobs#t=sq&q=j&li=20&l=false&jlo=en-US&j=apigee See my blog ...or my Github profile ...or my LinkedIn profile

Updated on February 05, 2020

Comments

  • Cheeso
    Cheeso over 4 years

    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.

  • Kim Gräsman
    Kim Gräsman almost 15 years
    Unfortunately I don't have a constant generator for Perl... Maybe it's time to take up development again :-)
  • Mr AH
    Mr AH over 14 years
    All my enums were working fine when the project was set as ComVisible(true), but how i've had to use the asm in a VS6 C++ project the tlb is too big!! I set the entire project as false and exposed the classes and enums I wanted. The classes are fine, the enums are not!!?? they are in the regsitry (CLSID) as expected, but are simply not accessible. Would be very interested to know why..
  • Jon O
    Jon O over 10 years
    Upvoted because I couldn't find any clear explanation that COM magically renames your enum values to preface them with the enum type's name, except for here.
  • venkat
    venkat over 5 years
    GuidAttribute is the class name for the [Guid] attribute. Using one or the other is the same. All attributes are declared as <name>Attribute, which then allows you to use it as just [<Name>].