将 C++ 枚举导入 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-01 09:01:50  来源:igfitidea点击:

Importing C++ enumerations into C#

提问by Mykroft

I'm currently working on creating a new C# project that needs to interact with an older C++ application. There is an error enumeration that already exists in the C++ app that I need to use in the C# app.

我目前正在创建一个需要与旧 C++ 应用程序交互的新 C# 项目。我需要在 C# 应用程序中使用 C++ 应用程序中已存在的错误枚举。

I don't want to just re declare the enumeration in C# because that could cause sync issues down the line if the files aren't updated together.

我不想只是在 C# 中重新声明枚举,因为如果文件没有一起更新,这可能会导致同步问题

All that being said my question is this: Is there a way for me to taken an enumeration declared like so:

说了这么多,我的问题是:有没有办法让我进行如下声明的枚举:

typedef enum
{
    eDEVICEINT_ERR_FATAL = 0x10001
    ...
} eDeviceIntErrCodes;

and use it in a C# program like so:

并在 C# 程序中使用它,如下所示:

eDeviceIntErrCodes.eDEVICEINT_ERR_FATAL

采纳答案by Brian Ensink

Check out the PInvoke Interop Assistant tool http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120. Its a useful tool for generating PInvoke signatures for native methods.

查看 PInvoke Interop Assistant 工具http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120。它是为本地方法生成 PInvoke 签名的有用工具。

If I feed it your enum it generates this code. There is a command line version of the tool included so you could potentially build an automated process to keep the C# definition of the enum up to date whenever the C++ version changes.

如果我将它提供给您的枚举,它会生成此代码。该工具包含一个命令行版本,因此您可以潜在地构建一个自动化过程,以便在 C++ 版本更改时使枚举的 C# 定义保持最新。


    public enum eDeviceIntErrCodes 
    {
        /// eDEVICEINT_ERR_FATAL -> 0x10001
        eDEVICEINT_ERR_FATAL = 65537,
    }

回答by Adam Haile

Simple answer is going to be no. Sorry, you are going to have to re-declare.

简单的答案是否定的。抱歉,您将不得不重新申报。

I have, in the past however, written scripts to import my C++ enums to a C# format in a enums.cs file and run it as part of the build, that way everything syncs.

然而,我过去曾编写脚本将我的 C++ 枚举导入 enums.cs 文件中的 C# 格式,并将其作为构建的一部分运行,这样一切都会同步。

回答by Joel Lucsy

If you had declared the enum like:

如果您已将枚举声明为:

namespace blah
{
    enum DEVICE_ERR_CODES
    {
        eDEVICEINT_ERR_FATAL = 0x10001,
        eDEVICEINT_ERR_OTHER = 0x10002,
    };
}

and in another file:

并在另一个文件中:

DEVICE_ERR_CODES eDeviceIntErrCodes;

and named the enum file with a .cs extension, you might be able to get it to work. You'd reference it like:

并使用 .cs 扩展名命名枚举文件,您也许可以让它工作。你会像这样引用它:

DEVICE_ERR_CODES err = DEVICE_ERR_CODES.eDEVICEINT_ERR_FATAL;

回答by Rob

In C/C++ you can #include a .cs file which contains the enumeration definition. Careful use of preprocessor directives takes care of the syntax differences between C# and C.

在 C/C++ 中,您可以 #include 包含枚举定义的 .cs 文件。谨慎使用预处理器指令可以解决 C# 和 C 之间的语法差异。

Example:

例子:

#if CSharp
namespace MyNamespace.SharedEnumerations
{
public
#endif


enum MyFirstEnumeration
{
    Autodetect = -1,
    Windows2000,
    WindowsXP,
    WindowsVista,
    OSX,
    Linux,

    // Count must be last entry - is used to determine number of items in the enum
    Count
};
#if CSharp
public 
#endif

enum MessageLevel
{
    None,           // Message is ignored
    InfoMessage,    // Message is written to info port.
    InfoWarning,    // Message is written to info port and warning is issued
    Popup           // User is alerted to the message
};

#if CSharp
    public delegate void MessageEventHandler(MessageLevel level, string message);
}
#endif

In your C# project, set a conditional compilation symbol "CSharp", make sure no such preprocessor definition exists in the C/C++ build environment.

在您的 C# 项目中,设置条件编译符号“CSharp”,确保 C/C++ 构建环境中不存在此类预处理器定义。

Note that this will only ensure both parts are syncronised at build time. If you mix-and-match binaries from different builds, the guarantee fails.

请注意,这只会确保两个部分在构建时同步。如果您混合匹配来自不同版本的二进制文件,则保证失败。

回答by doosik71

If you define strong enum in C++/CLI, enum codes will be included in the dll meta data. So, you can use enum codes in C#.

如果在 C++/CLI 中定义强枚举,则枚举代码将包含在 dll 元数据中。因此,您可以在 C# 中使用枚举代码。

public enum class eDeviceIntErrCodes: int
{
    eDEVICEINT_ERR_FATAL = 0x10001
    ...
};