views:

309

answers:

7

Hi All. I have a .NET application which serializes an object in binary format. this object is a struct consisting of a few fields.

I must deserialize and use this object in a C++ application. I have no idea if there are any serialization libraries for C++, a google search hasn't turned up much.

What is the quickest way to accomplish this?

Thanks in advance. Roey.

Update : I have serialized using Protobuf-net , in my .NET application, with relative ease. I also get the .proto file that protobuf-net generated, using GetProto() command. In the .proto file, my GUID fields get a type of "bcl.guid", but C++ protoc.exe compiler does not know how to interpret them! What do I do with this?

+1  A: 

Both boost and Google have libraries for serialization. However, if your struct is pretty trivial, you might consider managing the serialization yourself by writing bytes out from C# and then reading the data in C++ with fread.

Eric
When I binary serialize in .NET, does it not write bytes in a way that would be readable with C++ fread?
Roey
Oh I see what you meant, you need me to read each field seperately from the stream and put it into a variable in my C++ program....
Roey
Yes, and if your structure is simple enough with LayoutKind.Sequential, you could probably just blit the whole thing out in .NET and read it one go in in C++. This is assuming you're only using PODs like int, double, etc in your struct.
Eric
+5  A: 

Can you edit the .NET app? If so why not use XML Serialization to output the data in a easy to import format?

jamone
Probably the easiest way... even though XML is evil.
Polaris878
A: 

Do you have the option of changing the format? If so, consider choosing a non-binary format for greater interoperability. There are plenty of libraries for reading and writing XML. Json is popular as well.

Binary formats are efficient, but vulnerable to implementation details (does your C++ compiler pack data structures? how are ints and floats represented? what byte ordering is used?), and difficult to adjust if mangled. Text based formats are verbose, but tend to be much more robust. If you are uncertain about binary representations, text representations tend to be easier to understand (apart from challenges such as code pages and wide/narrow characters...).

For C++ XML libraries, the most capable (and perhaps also most complex) would still seem to be the Xerces library. But you should decide for yourself which library best fits your needs and skills.

Pontus Gagge
I am compiling both .NET and c++ with VS2008, I do not know the answers to your questions...If I use XML, can you please recommend a C++ library to deserialize it? If I decide to stick with binaries, can C++ understand a .NET serialized binary struct?
Roey
If you're not sure about the binary representations, you will run into trouble. See my edited answer.
Pontus Gagge
A: 

Serializing in a binary format and expecting an application in another language to read the binary is a very brittle solution (ie it will tend to break on the smallest change to anything).

It would be more stable to serialize the data in a common standard format.

Martin York
As long as the binary is in an agreed format, this doesn't have to be brittle *at all*. But yes - things like `BinaryFormatter` are doomed to fail here.
Marc Gravell
@Mark: Yes and no. Formats are agreed upon based on the binary representation of the structure. If anything changes like the endianess of an integer (change in hardware) padding between elements (update in compiler) etc the biary structure now no longer matches the original. A straight port of the C++ program with a new compiler or on a new machines has the potential to break. The worst thing is that a standard implementation would not be able to detect the change and may silently do the wrong thing.
Martin York
+1  A: 

Agree with others. You are making your app very vulnerable by doing this. Consider the situation if one of the classes you're serializing is changed in any way or built on a later version of the C# compiler: Your serialized classes could potentially change causing them to be unreadable.

An XML based solution might work well. Have you considered SOAP? A little out of fashion now but worth a look. The main issue is to decouple the implementation from the data. You can do this in binary if speed / efficiency is an issue, although in my experience, it rarely is.

Robin Welch
+3  A: 

If you are using BinaryFormatter, then it will be virtually impossible. Don't go there...

Protocol buffers is designed to be portable, cross platform and version-tolerant (so it won't explode when you add new fields etc). Google provide the C++ version, and there are several C# versions freely available (including my own) - see here for the full list.

Small, fast, easy.

Note that the v1 of protobuf-net won't handle structs directly (you'll need a DTO class), but v2 (very soon) does have tested struct support.

Marc Gravell
Hi Marc, sorry for abusing this comment section, but I've run into an issue with protobuf-net - could you take a look at my post and maybe answer? thanks :) http://stackoverflow.com/questions/2370090/protobuf-net-serializing-net-guid-how-to-read-this-in-c
Roey
@Roey - cheers for drawing my attention to it, but I already have an RSS feed for anything tagged `protobuf-net` ;-p
Marc Gravell
A: 

Use XML Serialization its the best way to go, in fact is the cleanest way to go.

XmlSerializer s = new XmlSerializer( typeof( YourClassType ) );
TextWriter w = new StreamWriter( @"c:\list.xml" );
s.Serialize( w, yourClassListCollection );
w.Close();
JeremySpouken