Murphy316
Murphy316

Reputation: 766

Passing a C++ object to C#

I have a C# application that is receiving serialized data as text from a C++ application (The data was serialized in C++ using the BOOST Library) Now the serialized data is actually an object which I need to convert to a similar .Net Object.In order to achieve this I have been told that I could write a C++/CLI component as a dll which could deserialize the text data (using the same library that was used to serialize it) and pass it back to the C# application. I have no experience programming in C++/CLI but I dont have any problem programming in C++.But I dont think C++/CLI would be difficult to catch on. My question is I dont get the part on how creating a C++/CLI application will pass the object back to C# ? Any hints , tutorials , links would greatly be appreciated

Edit the object being sent is something like this in C++

class person 
{ 
public: 
    person() 
    { 
    } 

    person(int age) : age_(age) 
    { 
    } 

    int age() const 
    { 
        return age_; 
    } 

private: 

    friend class boost::serialization::access; 

    template <typename Archive> 
    void serialize(Archive &ar, const unsigned int version) 
    { 
        ar & age_; 
    } 

    int age_; 
}; 

Upvotes: 4

Views: 3084

Answers (2)

Benjamin
Benjamin

Reputation: 900

I could be wrong, but isn't the whole point of XML that it's platform and language independent? As long as you're receiving C# class mirrors the data structure in the XML file (ie it has an int age_ variable), You shouldn't have any trouble using the normal XML file parser in C# to deserialize it. The fact that the XML file was written by a C++ application is no more relevant than if you wrote it by hand.

Upvotes: 0

Murtuza Kabul
Murtuza Kabul

Reputation: 6524

You should do some reading for namespace System.Runtime.InteropServices.Marshal

Here is an excellent article on how to marshal binary data - structures to c# structs. http://www.codeproject.com/Articles/66243/Marshaling-with-C-Chapter-3-Marshaling-Compound-Ty

Here is the type reference - http://www.codeproject.com/Articles/66244/Marshaling-with-C-Chapter-2-Marshaling-Simple-Type

It is called marshalling whereby you read binary or textual data in structs or complex types using C#.

Several things you should consider here is packing and alignment of structures. googling these terms will yield very helpful results

Upvotes: 1

Related Questions