c# - 如何通过互操作公开 COM 库中定义的枚举作为 C# 函数的返回类型

标签 c# enums com-interop

希望问题很清楚,但为了清楚起见,将其删除:

我有一个 VB6 dll,它定义了我在 C# dll 中引用的枚举。 C# dll 使用 idispatch 接口(interface)以正确的方式定义了一个 CCW,该接口(interface)声明了一个返回类型为枚举的函数。

当运行 regasm 时,我得到一个警告,指出枚举不是 COM 可见的,因此函数没有被导出。因为它是在我的 VB6 库中定义的,所以我认为它已经是 COM 可见的,因为它是在 COM dll 中定义的。

我意识到我可以停止搞乱并使用一个 int 来传递枚举并只对任一端进行强制转换,但这确实令人沮丧,我想知道是否存在一种方法。

这里是一些示例代码:

VB6 dll 定义一个枚举

Public Enum myEnum
    first = 0
    second = 1
End Enum

这通过互操作导入到 C# 中,如果您查看元数据,它看起来像这样

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

然后定义CCW接口(interface)

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

Regasm 提示 myEnum 不可见。我必须同意它,因为元数据 View 没有 com 可见属性。奇怪的是,如果我在 VB dll 中使用其他类型定义作为函数的参数,我没有收到任何投诉,它似乎是枚举,我猜这是因为我实际上公开了 VB6 枚举的互操作实现,而不是实际枚举。

所以我想我理解这个问题,我想知道是否有一种方法可以使用不涉及破解任何中间代码或自动生成代码的枚举来实现这一点。

最佳答案

看起来解决方案是为 C# 项目中导入的 COM 程序集将“嵌入互操作类型”属性设置为 False。

为了对此进行测试,我创建了一个 VB COM dll 作为 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

然后我创建了一个 C# 类项目并导入了这个 StackOverflow COM 库。然后,C# 中的以下代码创建一个 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; } }
    }
}

如果我们构建它,然后尝试使用 regasm/tlb:EnumDemo.tlb bin\Debug\EnumDemo.dll 从该程序集创建类型库,然后我会收到有关使用非 COM 的警告可见值类型。但是,一旦对 VB COM dll 的引用将“嵌入互操作类型”设置为 false,警告就会消失,并且使用 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);
    };
    ...

关于c# - 如何通过互操作公开 COM 库中定义的枚举作为 C# 函数的返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11647647/

相关文章:

vb.net - 使用 DLL 中的类创建 VB6 应用程序,然后在构建后换出该 DLL?

c# - 如何在 WPF 应用程序中放置自定义 Windows 窗体控件?

c++ - 枚举类的基础类型别名为整数类型(编译错误)

java - Java中的枚举查找

c# - 如何从 EventLogEntryType 枚举中获取 int 值

c# - 通过 COM Interop 创建时正确处理 C# 对象

com - ISequentialStream 的 RemoteRead 和 RemoteWrite 成员是什么?

c# - 在 C# 中从 Msi 中的自定义操作强制重启

c# - 带有 IIS 6 的 MVC

c# - 如何处理内联声明的对象