views:

1222

answers:

6

Is there an easy way to serialize data in c++ (either to xml or binary), and then deserialize the data in C#?

I'm working with some remote WINNT machines that won't run .Net. My server app is written entirely in C#, so I want an easy way to share simple data (key value pairs mostly, and maybe some representation of a SQL result set). I figure the best way is going to be to write the data to xml in some predefined format on the client, transfer the xml file to my server, and have a C# wrapper read the xml into a usable c# object.

The client and server are communicating over a tcp connection, and what I really want is to serialize the data in memory on the client, transfer the binary data over the socket to a c# memory stream that I can deserialize into a c# object (eliminating file creation, transfer, etc), but I don't think anything like that exists. Feel free to enlighten me.

Edit

I know I can create a struct in the c++ app and define it in c# and transfer data that way, but in my head, that feels like I'm limiting what can be sent. I'd have to set predefined sizes for objects, etc

+3  A: 

Protocol Buffers might be useful to you.

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages – Java, C++, or Python.

.NET ports are available from Marc Gravell and Jon Skeet.

Michael Myers
Looks useful, I guess the serialized data is just JSON? That's what it looks like.
scottm
No, it's binary. See http://code.google.com/apis/protocolbuffers/docs/encoding.html.
Michael Myers
This is what I'm going to check into. thanks!
scottm
A: 

C++ doesn't have structural introspection (you can't find out the fields of a class at runtime), so there aren't general mechanisms to write a C++ object. You either have to adopt a convention and use code generation, or (more typically) write the serialisation yourself.

There are some libraries for standard formats such as ASN.1, HDF5, and so on which are implementation language neutral. There are proprietary libraries which serve the same purpose (eg protocol buffers).

If you're targeting a particular architecture and compiler, then you can also just dump the C++ object as raw bytes, and create a parser on the C# side.

Quite what is better depends how tightly coupled you want your endpoints to be, and whether the data is mainly numerical (HDF5), tree and sequence structures (ASN.1), or simple plain data objects (directly writing the values in memory)

Pete Kirkham
A: 

Other options would be:

  • creating a binary file that contains the data in the way you need it ( not a easy & portable solution )

  • XML

  • YAML

  • plain text files

Geo
A: 

There are a lot of options you can choose from. Named pipes, shared memory, DDE, remoting... Depends on your particular need.

Quick googling gave the following:

Named pipes

Named Shared Memory

DDE

Vinay
A: 

As mentioned already, Protocol Buffers are a good option.

If that option doesn't suit your needs, then I would look at sending the XML over to the client (you would have to prefix the message with the length so you know how much to read) and then using an implementation of IXmlSerializer or use the DataContract/DataMember attributes in conjunction with the DataContractSerializer to get your representation in .NET.

I would recommend against using the marshaling attributes, as they aren't supported on things like List<T> and a number of other standard .NET classes which you would use normally.

casperOne
A: 

WCF is the .NET framework solution for serializing data it looks like this link figured out how to consume the data in c++, that's kind of the opposite direction of what you want, but there might be an easy analog for reversal.

http://geekswithblogs.net/cicorias/archive/2007/08/28/Consuming-WCF-Services-from-COM-using-C.aspx

iterationx