4

I've got a server written in C++ that sits at the end of a named pipe, um, serving things. The commands which can be sent to the server are defined in an enum which is located in a header file.

enum {
    e_doThing1,
    e_doThing2
    ...
    e_doLastThing
};

The value of the desired enum is put into the first byte of messages sent to the server so it knows what to do. I am now writing a C# client which needs access to the services.

Is there any way I can include the header into the C# code so I don't have to maintain the same list in two locations?

Thanks, Patrick

Patrick
  • 8,175
  • 7
  • 56
  • 72
  • possible duplicate of [Sharing an enum from C#, C++/CLI, and C++](http://stackoverflow.com/questions/3240263/sharing-an-enum-from-c-c-cli-and-c) – Ben Voigt Aug 11 '10 at 14:38

4 Answers4

3

If you were to put the enum in a namespace and give it a name, you could probably just add the header file directly to the C# project.

Edited with final solution:
That way around won't work, but the reverse will - name the header a .cs and include in C# project, then #include from C++.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Nice idea, you don't need the namespace but you do need to rename the file as a *.cs otherwise the compiler doesn't see the enum (the enum already had a name). Shame it couldn't be that simple :( – Patrick Aug 11 '10 at 12:29
  • 2
    @Patrick: Nothing stops the C++ compiler #including a .cs file. – Puppy Aug 11 '10 at 13:24
1

Can you generate a copy of the file with an automated process like a pre-build step defined on the C# project? All you need to do is create a copy of the file that adds a name between "enum" and "{", I think.

Edit: Depending on what kind of interface your C++ server exposes, .NET may even be able to automatically generate an interface file. For example, if this enum were defined in a COM interface or a .NET remoting interface, I think there would be automatic means of generating the enum on the client based on the server's compiled code.

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
1

You can use managed C++ for that. Include the header in a managed C++ project and you can use it from C#.

codymanix
  • 28,510
  • 21
  • 92
  • 151
0

You can use macro definition like this: on C# wrapper:

public enum class eAlgId
 {
   ENUM_PARAMETERS
 };

on cpp:

enum ProcAlgID
{
 ENUM_PARAMETERS 
};

and on .h file that is known to both:

#define ALG_ENUM_PARAMETERS \

et1 = 0, et2, et3, et4

Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
Cony
  • 1