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

1

4 Answers 4

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++.

Sign up to request clarification or add additional context in comments.

2 Comments

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: Nothing stops the C++ compiler #including a .cs file.
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.

Comments

1

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

Comments

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

Comments

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.