1

I am trying to access an enum in a C++ class header (.h) from C#. Is there a simple way of doing this? Every example I have seen involves compiling the class into a .dll, which for this example is kind of a waste because the function of this class is pretty limited.

Edit by request: The C++ library CAN be modified. In fact, the C++ library is the major component of the project(firmware) and is developed by other members of my team, the C# (my part) is just for unit testing purposes.

8
  • 2
    Does it ever change? The easiest (if you don't need to interop b/w C# and C++) would be to just replicate the Enum in C#. Commented Jan 4, 2013 at 16:14
  • As stanley said, just create a similar enum in C#. Only drawback is that if someone changes the C++ enum, the corresponding changes has to be made in C# class. Commented Jan 4, 2013 at 16:15
  • 1
    Have you considered writing a small script that parses the .h file and generates a .cs file with the enum? (e.g., using T4 Templating) Commented Jan 4, 2013 at 16:15
  • It is possible that it would change, which is why we want to make the C# code read from the C++ code, if you will. One of the developers changed the values in C++, but forgot to update the values in C#, and this is the reason the unit tests failed... Commented Jan 4, 2013 at 16:16
  • @dtb, I have not actually thought about that... it does seem like an easier solution than actually linking the code. Commented Jan 4, 2013 at 16:18

3 Answers 3

7

Realistically, you have three options:

  1. Create a glue library that exports a C function (extern "C") that includes this header and returns the enum value you are interested in, then P/Invoke this library from C#.
  2. Hard-code the value in your C# code.
  3. Write a C++ parser and extract the value that way. (Either a complete C++ parser, which would be preferable, or you can be lazy and write a simple regex parser that will work on this particular version of the C++ header, but might break in the future if the code changes in a way you didn't expect.)
Sign up to request clarification or add additional context in comments.

3 Comments

4. note that the enum Foo { base=1, next, nextnext, blah }; syntax is common between the two. Create a file that contains just the enum. #include it in a C++ header file where you need it. Now you don't have to fully parse C++, just manage to get a file containing an enum definition into your C# project.
@cdhowie, want to copy that comment into the answer?
The question does not specify whether the C++ header is part of the same codebase or not. Therefore I'm trying to keep my answer to just those techniques that work with an external C++ library -- that is, one that cannot be easily modified. The people who land here from a Google search are more likely to be in this situation with a third-party library. If the OP edits the question to note that the C++ library is part of the same codebase and therefore can be modified as part of the solution, then I would definitely be willing to incorporate that technique into my answer.
0

In order to access the C++ enum, you have to turn the C++ class into something that .NET can recognise, which either means compiling it as C++/CLI (if that is an option) or turning into a DLL that the .NET interop can access.

Comments

0

Well, this freaks the CS pre-compiler out a bit, but it builds:

#if CSHARP

namespace Test
{
    public enum SharedEnum

#endif //CSHARP

#if CPP
    typedef enum SharedEnum 
#endif //CPP
    {
        One,
        Two,
        Three
    }
#if CPP
    SharedEnum 
#endif //CPP
    ;
#if CSHARP
};
#endif

Just add the Conditional Compilation symbol CSHARP to your C# project, add the existing .cs file and the preprocessor definition CPP to your C++ project.

(credit to Yakk for having the same idea)

1 Comment

Can be simplified: C++, C#

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.