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
Sheng Jiang[MVP] - 03 Aug 2007 01:24 GMT
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.

Signature
Sheng Jiang
Microsoft MVP in VC++
> Hi everyone,
>
[quoted text clipped - 11 lines]
> Regards
> Dean Mitchell
Ben Voigt [C++ MVP] - 03 Aug 2007 18:01 GMT
> 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
[quoted text clipped - 19 lines]
> #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
};
Pixel.to.life - 03 Aug 2007 01:56 GMT
> Hi everyone,
>
[quoted text clipped - 11 lines]
> 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.