导入C ++枚举到C# [英] Importing C++ enumerations into C#

查看:148
本文介绍了导入C ++枚举到C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我不希望只是重新申报枚举在C#中,因为这可能会导致同步问题的路线,如果文件没有一起更新的。



之所以这么说,我的问题是这样的:
有没有办法,我采取了一个枚举声明,像这样:

 的typedef枚举
{
eDEVICEINT_ERR_FATAL = 0x10001

} eDeviceIntErrCodes;

和使用它的C#程序像这样:

  eDeviceIntErrCodes.eDEVICEINT_ERR_FATAL 


解决方案

退房的PInvoke互操作的助手工具 http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120 。其产生的PInvoke签名本机方法的有用工具。



如果我给它的枚举它生成的代码。还有就是工具包含,所以你可能会建立一个自动化的过程,以保持枚举的C#定义最新每当C ++版本变更的命令行版本。

  
公共枚举eDeviceIntErrCodes
{
/// eDEVICEINT_ERR_FATAL - > 0x10001
eDEVICEINT_ERR_FATAL = 65537,
}


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.

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.

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:

eDeviceIntErrCodes.eDEVICEINT_ERR_FATAL

解决方案

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.

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.


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

这篇关于导入C ++枚举到C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆