1

I want to send a struct in C# to C++ using sockets.

For example, I use this struct:

[StructLayout(LayoutKind.sequential, Pack = 1)]
struct pos {
    public int i;
    public float x;
};

If I somehow convert it into bytes and send over the network, I should be able to cast it to this in c++:

struct pos {
    int i;
    float x;
};

... I think.

1) how do you break down a struct instance in C# to send it over the network?

2) can I safely cast it to the c++ struct once I get it?

Thanks

2
  • Personally I'd be looking at known serialization rather than snagging the underlying bytes; in partiular, strings are going to be a royal pain otherwise. Commented Apr 8, 2011 at 12:08
  • I'm just going to be using floats and ints if it helps Commented Apr 8, 2011 at 12:10

2 Answers 2

5

The marshaller helps you with converting between .NET structs and raw bytes. In this answer, I posted a simple solution, which boils down to Marshal.StructureToPtr and Marshal.PtrToStructure. In contrast to the more advanced solutions provided by Johann du Toit, this is in my opinion the best thing you can do if all you want to do is to push some structures through a byte stream.

If you do this, you can safely cast to the C++ struct if the length is correct, and your C++ struct is declared with the same packing as the C# struct (i.e. #pragma pack in VC++ or __attribute__((packed)) in GCC).

Note that this also works with fixed length C strings, but will not take care of the endianness of larger values. I found it a simple solution to provide getters and setters for the latter problem which just swap the bytes accordingly (with BitConverter).


Some elaboration on the packing: Take the following structure:

struct MyStruct {
    uint8_t a;
    float b;
};

With the C# declaration with StructLayout, Pack = 1, this struct will have a size of five bytes. The C++ struct, however, may have eight bytes (or even more), depending on the default packing of the compiler, who may happily insert some padding bytes to align the float value on a 32-bit boundary (just an example). Because of this, you have to apply the very same packing to both the C# and C++ struct. In Visual C++:

#pragma pack(push, 1)
// ... struct declarations...
#pragma pack(pop)

This means all structs declared between the two pragmas will have a packing of one. In GCC:

struct x {
// ...
} __attribute__((packed));

This will do the same. You can #define __attribute__(x) on Windows platforms and #ifdef _WIN32 around the pragmas to make the code compatible with both worlds.

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

10 Comments

can you explain the last part more? I'm not sure I follow what to do there
The one about packing, or the paragraph I just added one minute ago? :)
the packaging part :p also why do you have a maxlength in your function?
I see, but if I don't mind the padding, could I just remove the Pack = 1 and then cast safely in C++?
If I remember correctly, the maxlength was about variable length structs. I also had an overload the passes Marshal.SizeOf(value) as maxlength :)
|
1

You can either encode it in a format like JSON (There are a lot of JSON parsers out there, check on the json.org website for a list), XML or just roll your own. You could also try already built libraries like Protobuf, which allows you to serialize your structures that you would create with a file in .proto format (And use Protobuf-Net for C#). Another option would be Thrift which provides a way to serialize but also supplies a ready to use RCP system. It support c#, c++ and a ton of other languages by default.

So it's depends on taste, take your pick :D

3 Comments

do you have to use a 3rdpart library? I just want something lightweigth and simple
No you don't have to, it's just easier as they handle all the grease work for you so you can focus on your application.
+1 for some good pointers in this answer, though it may be overkill for a lot of applications.

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.