Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing C++ enumerations into C#

Tags:

c++

c#

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
like image 325
Mykroft Avatar asked Aug 20 '08 20:08

Mykroft


People also ask

Can you use enum in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

Can we assign value to enum in C?

An enum is considered an integer type. So you can assign an integer to a variable with an enum type.

How are enumeration variables declared in C?

Enumeration is a user defined datatype in C language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration.

How are enums stored in C?

Enumeration "values" aren't stored at all, as they are compile-time named constants. The compiler simply exchanges the use of an enumeration symbol, with the value of it.


3 Answers

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.

like image 130
Rob Avatar answered Oct 02 '22 15:10

Rob


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,
    }
like image 12
Brian Ensink Avatar answered Sep 30 '22 15:09

Brian Ensink


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.

like image 2
Adam Haile Avatar answered Oct 03 '22 15:10

Adam Haile