Exposing a c++ enum in a managed c++ assembly

D

Dean Mitchell

Hi everyone,

We have a c++ server application that we are writing a GUI client
application for. To save our time and to avoid duplicating all the code and
functionality that already exists in c++ classes I am building a managed c++
assembly around these classes so that a c# program can use them.

The problem is that alot of these c++ classes use enums, I would like to
make these visible to the c# application but I cannot find a way to do this.
Do I have to duplicate them so that I have one lot for C++ and the other for
dotnet? There must be an easier way?

Thanks for your time
Regards
Dean Mitchell
 
S

Sheng Jiang[MVP]

you need to declare the enum using the enum class keyword, you can put the
enum entries in a #if block to generate the managed or native version of the
enum from the same file.
//enumsample.h
#if defined(_MANAGED)
enum class className
{
#else
enum {
#endif
.......
#if defined(_MANAGED)
}
#else
} className
#endif

//enumsample.cpp
#undef _MANAGED
#include "enumsample.h"
#defefine _MANAGED
#include "enumsample.h"

Another method is to create a type library and #import it from C++,
reference it from your managed projects.
 
P

Pixel.to.life

Hi everyone,

We have a c++ server application that we are writing a GUI client
application for. To save our time and to avoid duplicating all the code and
functionality that already exists in c++ classes I am building a managed c++
assembly around these classes so that a c# program can use them.

The problem is that alot of these c++ classes use enums, I would like to
make these visible to the c# application but I cannot find a way to do this.
Do I have to duplicate them so that I have one lot for C++ and the other for
dotnet? There must be an easier way?

Thanks for your time
Regards
Dean Mitchell

Dean,

I assume the enums are used at the API level (the interface between
unmanaged/managed code), so whenever you want to transfer control from
managed app to an unmanaged C++ method, you have to pass one of these
enum values in too. I faced a similar situation, and had these
options:

[1] pass in int values, cast to the enumns on the unmanaged side (I
wont recommend it)
[2] wherever you expose and wrap the unmanaged classes by managed
wrappers, just add enums with the class. If the enums are within the
unmanaged classes' scope, you can wrap them as and with you wrap the
classes. No?

I went the second way.
 
B

Ben Voigt [C++ MVP]

Sheng Jiang said:
you need to declare the enum using the enum class keyword, you can put the
enum entries in a #if block to generate the managed or native version of
the
enum from the same file.
//enumsample.h
#if defined(_MANAGED)
enum class className
{
#else
enum {
#endif
......
#if defined(_MANAGED)
}
#else
} className
#endif

//enumsample.cpp
#undef _MANAGED
#include "enumsample.h"
#defefine _MANAGED
#include "enumsample.h"

Very close. Fixing the syntax mistakes and simplifying:

#if _MANAGED
#define ENUMKEYWORD enum class
#else
#define ENUMKEYWORD enum
#endif

ENUMKEYWORD enumName
{
A = 0,
B,
C
};
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top