Question

I need to pass a rather complicated data structure from C# to a native C++ function. This is a structure, which besides of simple data types like ints and strings, also contains arrays of other structures, containing arrays of other structures ... and so on, up to 3 or 4 levels deep. My initial idea was to use pinned pointers, but now I'm starting to doubt that it was a good idea. The code is messy and I'm keeping a large number of objects pinned for a long time. I have to say that the C++ code can take many hours to finish.

Now to my question. Would it be a good idea to use google's protocol buffers to serialize the data structure in C#, put it inside a continuous memory buffer and then deserialize it in C++?

Any other suggestions?

No correct solution

OTHER TIPS

Another option is to use the Marshal class to allocate and write data into your complex structure. For instance you can allocate a buffer using AllocHGlobal and then use the various copy and write functions to write data into this buffer using whatever memory layout you desire. This allows you to keep your C++ code unchanged.

I would suggest a common serialization/deserialization, such as XML. Of course, some methods, such as XML serialization, will only serialize the public properties of your objects, so choose appropriately.

You got a few options.

COM, managed C++, binary serialization and marshalling. I'd go for managed C++ if portability was not an issue. And second - binary serialization. Easier to maintain.

So yes, protocol buffers is definitely a viable solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top