简体   繁体   English

C++ 中的序列化和 C# 中的反序列化,反之亦然,命名为 pipe

[英]Serialization in C++ and Deserialization in C# and vice versa for named pipe

I have two process(.exe), once is implemented in C++ and another is implemented in C#.我有两个进程(.exe),一个在 C++ 中实现,另一个在 C# 中实现。 what I want is to communication between both the process.我想要的是两个过程之间的通信。 for that I have choose "Named Pipe" approach.为此,我选择了“命名管道”方法。 the issue which I am facing is to pass a class object from one application to another.我面临的问题是将 class object 从一个应用程序传递到另一个应用程序。 I am easily able to send a string data but I don't know how to send a class object from application to another.我可以轻松发送字符串数据,但我不知道如何将 class object 从应用程序发送到另一个应用程序。 my class structure is like below:我的 class 结构如下:

enum information
{
 TITLE,
 STATUS,
 DEBUG,
 INFO,
 OTHER
};

class CInformation
{
  private:
    information _info;
    string _text;
  public:
    void setInformation(information info, string text) { _info = info; _text = text; }
    inline information GetInfo() { return _info; }
    inline string GetText() { return _text; }
};

so how to Serialize this class in C++, send it over named pipe and Deserialize it in C# and vice versa also for the same class. so how to Serialize this class in C++, send it over named pipe and Deserialize it in C# and vice versa also for the same class. please give me an example.请给我一个例子。 I am open for any other solution also for two way communication between both the process.我对任何其他解决方案也持开放态度,也可以在两个过程之间进行双向通信。

Just as you chose named pipes you must now choose a serialization method/convention.正如您选择命名管道一样,您现在必须选择一种序列化方法/约定。 If your C++ code has access to.Net through CLI then you are off to a good start because you can ensure that the same rules are applied at both ends.如果您的 C++ 代码可以通过 CLI 访问 .Net,那么您将有一个良好的开端,因为您可以确保在两端应用相同的规则。 Otherwise you have options like JSON, XML, simple text strings or even binary.否则,您可以选择 JSON、XML、简单的文本字符串甚至二进制。 You may have a technical reason for making this choice (data and structure vs compactness etc) or it might be driven by the availability of tools/frameworks (probably steering you towards JSON or XML) to do (de)serialization for you.您可能有做出此选择的技术原因(数据和结构与紧凑性等),或者它可能由工具/框架的可用性驱动(可能引导您转向 JSON 或 XML)为您进行(反)序列化。 Note that even with simple text strings you will have to make sure that both sides use the same encoding (UTF-8, ASCII, ...).请注意,即使使用简单的文本字符串,您也必须确保双方使用相同的编码(UTF-8、ASCII、...)。

Note that C++ and C# make no "language guarantees" to implement classes in any "compatible" way, it is up to you to map the two views to each other and/or structure the classes to have - for example - similar JSON-serialized layouts.请注意,C++ 和 C# 不做任何“语言保证”以任何“兼容”方式实现类,这取决于 map 两个视图彼此和/或 JSON 序列化类的示例结构布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM