Serialize in C++ then deserialize in C#?

前端 未结 6 585
轮回少年
轮回少年 2020-12-15 09:40

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\

相关标签:
6条回答
  • 2020-12-15 10:00

    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

    0 讨论(0)
  • 2020-12-15 10:04

    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.

    0 讨论(0)
  • 2020-12-15 10:06

    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

    0 讨论(0)
  • 2020-12-15 10:07

    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.

    0 讨论(0)
  • 2020-12-15 10:17

    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)

    0 讨论(0)
  • 2020-12-15 10:19

    I checked out all mentioned projects like prottocol buffers, json, xml, etc. but after I have found BSON I use this because of the following reasons:

    • Easy to use API
    • Available in many languages (C, C++, Haskell, Go, Erlang, Perl, PHP, Python, Ruby, C#, ...)
    • Binary therefore very space efficient and fast (less bytes->less time)
    • constistent over platforms (no problems with endianess, etc)
    • hierarchical. The data model is comparable to json (what the name suggests) so most data modelling tasks should be solvable.
    • No precompiler necessary
    • wideley used (Mongodb, many languages)
    0 讨论(0)
提交回复
热议问题