How to output an enum defined in a COM library via interop as a return type of a C # function

Hopefully the question will be clear, but for clarity:

I have a VB6 dll that defines an enum which I reference in my C # dll. The C # dll defines CCW in the right way with the idispatch interface, which declares a function that returns a type - this is an enumeration.

When I run regasm, I get a warning that the enum is not COM visible, and therefore the function is not exported. Since it is defined in my VB6 library, I would think that it is already COM visible, since it is defined in the COM dll.

I understand that I can stop messing around and use int to pass the enumeration, and just do the casting, but this is inconvenient, and I would like to know if there is a way.

As requested, an example code is provided:

VB6 dll single enumeration

Public Enum myEnum
    first = 0
    second = 1
End Enum

This is imported via interop in C #, and if you are viewing metadata, it looks something like this.

[Guid("EnumGUID")]
public enum myEnum
{
    first = 0,
    second = 1        
}

Then the CCW interface is defined

[ComVisible(true)]
[Guid("InterfaceGuid")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMyCCWInterface
{
    [DispId(1)]
    myEnum myFunction();
}

Regasm complains that myEnum is not visible. I have to agree with this, since the metadata view does not have the com visible attribute. It is strange if I use other types that are defined in the VB dll for function arguments, I do not receive any complaints, this is just an enumeration, and I suppose this because I will actually make public the inline implementation of the VB6 enumeration, and not the actual enumeration.

So, I think I understand the problem, that I would like to know if there is a way to get this work to use enums that are not related to hacking any intermediate or automatically generated code.

+5
2

, , " " False COM #.

, dll VB COM StackOverflow.ExampleCom

Public Enum EThing
    eThingOne = 1
    eThingTwo = 2
End Enum
Private mThing As EThing
Private Sub Class_Initialize()
    mThing = eThingOne
End Sub
Public Property Let Thing(newVal As EThing)
    mThing = newVal
End Property
Public Property Get Thing() As EThing
    Thing = mThing
End Property

# COM StackOverflow. # COM-, , VB, , OP.

using System;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using StackOverflow;

namespace EnumDemo
{
    [ComVisible(true)]
    [Guid("c30d35fe-2c7f-448b-98be-bd9be567ce70")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface IEnumDemo
    {
        [DispId(1)]
        EThing Thing
        {
            get;set;
        }
    }

    [ComVisible(true)]
    [Guid("af328c82-08e3-403e-a248-8c46e27b48f3")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("StackOverflow.EnumDemo")]
    public class EnumDemo
    {
        private EThing mThing = EThing.eThingOne;
        public EThing Thing { get { return mThing; } set { mThing = value; } }
    }
}

, typelib regasm /tlb:EnumDemo.tlb bin\Debug\EnumDemo.dll, , COM. , DLL VB COM "Embed Interop Types", false, typelib OleView , importlib dll.

library EnumDemo
{
    // TLib :     // TLib :  : {D482D5CB-EE6C-455A-A28A-D26A5AC579D5}
    importlib("StackOverflow.dll");
    // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
    ...
    interface IEnumDemo : IDispatch {
        [id(0x00000001), propget]
        HRESULT Thing([out, retval] EThing* pRetVal);
        [id(0x00000001), propput]
        HRESULT Thing([in] EThing pRetVal);
    };
    ...
+2

. Problem - , - . . .NET ComVisible. EnumType_myEnum, myEnum (. this)

+1

All Articles