<p>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.</p>

<p><em>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</em>. </p>

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

<pre><code>typedef enum
{
    eDEVICEINT_ERR_FATAL = 0x10001
    ...
} eDeviceIntErrCodes;
</code></pre>

<p>and use it in a C# program like so:</p>

<pre><code>eDeviceIntErrCodes.eDEVICEINT_ERR_FATAL
</code></pre>

[{'answer_id': 18774, 'author': 'Adam Haile', 'author_id': 194, 'author_profile': 'https://Stackoverflow.com/users/194', 'pm_score': 2, 'selected': False, 'text': '<p>Simple answer is going to be no.  Sorry, you are going to have to re-declare.</p>\n\n<p>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.</p>\n'}, {'answer_id': 18786, 'author': 'Brian Ensink', 'author_id': 1254, 'author_profile': 'https://Stackoverflow.com/users/1254', 'pm_score': 5, 'selected': True, 'text': '<p>Check out the PInvoke Interop Assistant tool <a href="http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120" rel="noreferrer">http://www.codeplex.com/clrinterop/Release/ProjectReleases.aspx?ReleaseId=14120</a>.  Its a useful tool for generating PInvoke signatures for native methods.  </p>\n\n<p>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.</p>\n\n<pre><code>\n    public enum eDeviceIntErrCodes \n    {\n        /// eDEVICEINT_ERR_FATAL -> 0x10001\n        eDEVICEINT_ERR_FATAL = 65537,\n    }\n</code></pre>\n'}, {'answer_id': 18798, 'author': 'Joel Lucsy', 'author_id': 645, 'author_profile': 'https://Stackoverflow.com/users/645', 'pm_score': 0, 'selected': False, 'text': "<p>If you had declared the enum like:</p>\n\n<pre><code>namespace blah\n{\n    enum DEVICE_ERR_CODES\n    {\n        eDEVICEINT_ERR_FATAL = 0x10001,\n        eDEVICEINT_ERR_OTHER = 0x10002,\n    };\n}</code></pre>\n\n<p>and in another file:</p>\n\n<pre><code>DEVICE_ERR_CODES eDeviceIntErrCodes;</code></pre>\n\n<p>and named the enum file with a .cs extension, you might be able to get it to work.\nYou'd reference it like:</p>\n\n<pre><code>DEVICE_ERR_CODES err = DEVICE_ERR_CODES.eDEVICEINT_ERR_FATAL;</code></pre>\n"}, {'answer_id': 18870, 'author': 'Rob', 'author_id': 1006, 'author_profile': 'https://Stackoverflow.com/users/1006', 'pm_score': 4, 'selected': False, 'text': '<p>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.</p>\n\n<p>Example:</p>\n\n<pre><code>#if CSharp\nnamespace MyNamespace.SharedEnumerations\n{\npublic\n#endif\n\n\nenum MyFirstEnumeration\n{\n    Autodetect = -1,\n    Windows2000,\n    WindowsXP,\n    WindowsVista,\n    OSX,\n    Linux,\n\n    // Count must be last entry - is used to determine number of items in the enum\n    Count\n};\n#if CSharp\npublic \n#endif\n\nenum MessageLevel\n{\n    None,           // Message is ignored\n    InfoMessage,    // Message is written to info port.\n    InfoWarning,    // Message is written to info port and warning is issued\n    Popup           // User is alerted to the message\n};\n\n#if CSharp\n    public delegate void MessageEventHandler(MessageLevel level, string message);\n}\n#endif\n</code></pre>\n\n<p>In your C# project, set a conditional compilation symbol "CSharp", make sure no such preprocessor definition exists in the C/C++ build environment.</p>\n\n<p>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.</p>\n'}, {'answer_id': 51630980, 'author': 'doosik71', 'author_id': 3763893, 'author_profile': 'https://Stackoverflow.com/users/3763893', 'pm_score': 0, 'selected': False, 'text': '<p>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#.</p>\n\n<pre><code>public enum class eDeviceIntErrCodes: int\n{\n    eDEVICEINT_ERR_FATAL = 0x10001\n    ...\n};\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/18765', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2191/']