encapsulate c enum's inC++/CLI for C#

  • Thread starter Thread starter cartoper
  • Start date Start date
C

cartoper

I am writing C# code that interacts with a external device's firmware
which is writen in C. There is a common enum file the firmware code is
using that I would like to consume in the C# world. I understand that
C# cannot use C header files directly, so I thought I could wrap the
enum file in a C++/CLI assembly. So I have done something like this:

------------------------------------
#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
#endif

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

typedef enum toggleonoff{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

With hopes of getting to the toggleonoff enum this way:

MyCompany.InstrumentCommsEnums.toggleonoff.TOG_OFF

After referring the C++/CLI project in the C# code, there is no
MyCompany.InstrumentCommsEnums namespace. Any thoughts?
 
I am writing C# code that interacts with a external device's firmware
which is writen in C. There is a common enum file the firmware code
is using that I would like to consume in the C# world. I understand
that C# cannot use C header files directly, so I thought I could wrap
the enum file in a C++/CLI assembly. So I have done something like
this:

------------------------------------
#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
#endif

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

typedef enum toggleonoff{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

With hopes of getting to the toggleonoff enum this way:

MyCompany.InstrumentCommsEnums.toggleonoff.TOG_OFF

After referring the C++/CLI project in the C# code, there is no
MyCompany.InstrumentCommsEnums namespace. Any thoughts?

C++/CLI supports two kinds of enum:

enum foo
{
// traditional C/C++ enum
};

enum class bar
{
// .NET enum
};

Change your wrapper to something like

------------------------------------

#define CT_MAXZONE 0x7
#define CT_MAXCHANNEL 0x8
#define CT_ADCCHANNEL 0xB

#ifdef __cplusplus_cli
namespace MyCompany::InstrumentCommsEnums
{
enum class toggleonoff
#else
typedef enum toggleonoff
#endif
{
TOG_OFF = 0,
TOG_ON = 1
}TOGGLEONOFF;

#ifdef __cplusplus_cli
}
#endif
------------------------------------

.... and it might just work. Of course, you can also re-type the enum(s) in
..NET form, although there may be more chance of error along that route.

-cd
 
shouldn't the enum also be made public if you need it to be accessible by
others?

kind regards,
Bruno
 
I have the enum declared as public and I am including the enum.h file
in a enum.cpp file like the following code snippet and there is no
using namespace stuff in the header file anymore. The problem is I
still cannot see the MyCompany.InstrumentCommsEnums namespace in the C#
code.s

namespace MyCompany
{
namespace InstrumentCommsEnums
{

#include "enums.h"

}
}
 
I have the enum declared as public and I am including the enum.h file
in a enum.cpp file like the following code snippet and there is no
using namespace stuff in the header file anymore. The problem is I
still cannot see the MyCompany.InstrumentCommsEnums namespace in the C#
code.s

namespace MyCompany
{
namespace InstrumentCommsEnums
{

#include "enums.h"

}
}
 
I have the enum declared as public and I am including the enum.h file
in a enum.cpp file like the following code snippet and there is no
using namespace stuff in the header file anymore. The problem is I
still cannot see the MyCompany.InstrumentCommsEnums namespace in the C#
code.s

namespace MyCompany
{
namespace InstrumentCommsEnums
{

#include "enums.h"

}
}
 
I have the enum declared as public and I am including the enum.h file
in a enum.cpp file like the following code snippet and there is no
using namespace stuff in the header file anymore. The problem is I
still cannot see the MyCompany.InstrumentCommsEnums namespace in the C#
code.s

namespace MyCompany
{
namespace InstrumentCommsEnums
{

#include "enums.h"

}
}
 
Cleaned up the header file some more and I can see the namespace now.
Sorry for the four posts, Google didn't seem to be responding so I kept
hiting post, opps!
 
Back
Top