Groups | Blog | Home
all groups > dotnet interop > august 2007 >

dotnet interop : Exposing a c++ enum in a managed c++ assembly


Dean Mitchell
8/2/2007 12:00:00 AM
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

Ben Voigt [C++ MVP]
8/25/2007 1:32:02 AM

[quoted text, click to view]

Put the innards of the enum into a separate header file, then #include it
twice:

enum NativeEnum {
#include "enum.h"
};

enum class ManagedEnum {
#include "enum.h"
};

Or, someone else had a really nifty idea.

Write the enum in native form, then it's also a valid C# file. So your
native C++ code contains:

#include "enum.cs"

The C# code just has that file included in the project. If you want the
enum to be public in C#, you'll end up with something like:

#define public
#include "enum.cs"
#undef public
AddThis Social Bookmark Button