Question
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
Solution
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,
}
OTHER TIPS
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.
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.
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.
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.
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:
DEVICE_ERR_CODES err = DEVICE_ERR_CODES.eDEVICEINT_ERR_FATAL;
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#.
public enum class eDeviceIntErrCodes: int
{
eDEVICEINT_ERR_FATAL = 0x10001
...
};