using enums from C++ headers in C#

S

Steve Roberts

Hi,

I've got an unmanaged C++ DLL that I'm calling functions in from a C# EXE,
via a managed C++ DLL.
My problem is that I have a lot of enumerated types defined in C++ headers -
is there a way that I can use these enumerations in all of the modules,
rather than having to rewrite them for the C# ?

e.g. if I have:

enum ColorValue { red, green, blue };

in my C++ .h file, is there a way that I can use ColorValue in my C# code ?

Thanks for any help,
Steve
 
N

Nicholas Paldino [.NET/C# MVP]

Steve,

You could always declare the enumerations as managed enumerations, then
they would be accessible to any assembly that manages the referenced
assembly.

Basically, you would do:

enum class MyEnum {Value1, Value2};

I think this uses the new CLI bindings for C++. I am not sure what the
managed extensions for C++ syntax would be though.

Hope this helps.
 
P

Philip Rieck

Not automatically - you'll have to copy-paste them and compile them into a
..net assembly, use the integer values, or create a quick tool to do the
copy/paste/fix/compile steps for you. your c# code can't use the enums
directly from c++ . The syntax is so close (as in, the same) that you
should be able to do much of it easily, though
 

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