Sharing enums between C++ and C#

In your C# project  properties on the Build tab, add a conditional compilation symbol called C_SHARP

Then create a new file in the C# project to hold the Enums. Here I’ve created an enum called AlarmEnum in the namespace Models in a file called commonAlarms.cs

 

#if C_SHARP
using System;
namespace Models
{
public
#endif
enum AlarmEnum
{
//Common
AL_NONE = 0,
AL_PROCESS_ABORT =1,
AL_VACUUM_TRIP = 2,
AL_DOOR_OPEN = 3,
AL_AIR_FAIL = 4,
};
#if C_SHARP
}
#endif

 

Then in your C++ project, add a Header file whose contest is :

#include “commonAlarms.cs”

you will need to add the path to the include filename or add it to the Include Directories in the VC++ Directories page of the project properties. Now they can both use the same enum and should never get out of sync.

 

 

Add a Comment

Your email address will not be published. Required fields are marked *