C# 使用Protobuf.NET 需要在托管C和非托管C++之间发送一些数据。经过研究 我试着使用Protobuf.NET

C# 使用Protobuf.NET 需要在托管C和非托管C++之间发送一些数据。经过研究 我试着使用Protobuf.NET,c#,c++,serialization,protobuf-net,C#,C++,Serialization,Protobuf Net,我不确定我是否理解ProtoBuf的功能 在Proto中构建类型定义。在C++和C<是否可以定义C++中的类型来生成C++中的文件? 试图将命令行工具protogen.exe用于以下文件 test1.proto test2.proto message Person { required int32 id = 1; required string name = 2; optional string email = 3; } 没有什么对我有用。真的什么都试过了。我把原始文件放进了

我不确定我是否理解ProtoBuf的功能

  • 在Proto中构建类型定义。在C++和C<使用命令行工具“protogen.exe”从类型定义中获取.cs文件和.cpp.h
  • 将.cs文件复制到我的C++项目中,并在我的C++解决方案中.CP.,h。
  • 看来我解决这个问题太愚蠢了。这是我的问题和疑问

    < L>是否可以定义C++中的类型来生成C++中的文件?
  • 试图将命令行工具protogen.exe用于以下文件

  • test1.proto


    test2.proto

    message Person {
      required int32 id = 1;
      required string name = 2;
      optional string email = 3;
    }
    

    没有什么对我有用。真的什么都试过了。我把原始文件放进了 命令行目录,尝试了每个选项来设置目录。如何轻松构建它们?
    第二个文件是用C++的标准PROTO命令行工具工作,但我也需要C语言。真的需要你的帮助。

    虽然我对ProtoBuf了解不多,但我知道有一种更简单的方法。如果上面的类表示您试图发送给C++的数据,只需使用和序列化类,那么就可以将类和数据输出到XML文件。然后,您可以使用XML或其他XML读取器读入类和数据。您甚至可以使用来读取序列化的类。

    test1.proto
    不是有效的
    .proto
    文件


    test2.proto
    看起来不错。请注意,您需要从主ProtoBF项目中编译编译器,以便生成C++代码,并从ProtoBuF网络中编译编译器,以便生成C代码。我想你的大多数问题都是因为没有意识到你需要两个不同的编译器(但是你向两个编译器都提供了相同的
    .proto
    文件)。

    首先,请注意protobuf net只是.net的一个可用实现;无论如何

    “test1.proto”不是.proto-它是C#;protobuf net不需要使用.proto,但在您的互操作场景中,这是一个非常好的主意。有VS2010插件,或者protobuf net zip中的
    protogen
    工具:

     protogen -i:test2.proto -o:test2.cs
    
    这将生成包含以下内容的
    test2.cs

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    // Generated from: test2.proto
    namespace test2
    {
      [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Person")]
      public partial class Person : global::ProtoBuf.IExtensible
      {
        public Person() {}
    
        private int _id;
        [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
        public int id
        {
          get { return _id; }
          set { _id = value; }
        }
        private string _name;
        [global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"name", DataFormat = global::ProtoBuf.DataFormat.Default)]
        public string name
        {
          get { return _name; }
          set { _name = value; }
        }
    
        private string _email = "";
        [global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"email", DataFormat = global::ProtoBuf.DataFormat.Default)]
        [global::System.ComponentModel.DefaultValue("")]
        public string email
        {
          get { return _email; }
          set { _email = value; }
        }
        private global::ProtoBuf.IExtension extensionObject;
        global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
          { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
      }
    
    }
    
    //------------------------------------------------------------------------------
    // 
    //这段代码是由一个工具生成的。
    //
    //对此文件的更改可能会导致不正确的行为,如果
    //重新生成代码。
    // 
    //------------------------------------------------------------------------------
    //生成自:test2.proto
    名称空间test2
    {
    [global::System.Serializable,global::ProtoBuf.ProtoContract(Name=@“Person”)]
    公共部分类人员:全局::ProtoBuf.IExtensible
    {
    公众人物(){}
    私人内部id;
    [global::ProtoBuf.ProtoMember(1,IsRequired=true,Name=@“id”,DataFormat=global::ProtoBuf.DataFormat.twoscomplete)]
    公共整数id
    {
    获取{return\u id;}
    设置{u id=value;}
    }
    私有字符串\u名称;
    [global::ProtoBuf.ProtoMember(2,IsRequired=true,Name=@“Name”,DataFormat=global::ProtoBuf.DataFormat.Default)]
    公共字符串名
    {
    获取{return\u name;}
    设置{u name=value;}
    }
    私人字符串_email=“”;
    [global::ProtoBuf.ProtoMember(3,IsRequired=false,Name=@“email”,DataFormat=global::ProtoBuf.DataFormat.Default)]
    [global::System.ComponentModel.DefaultValue(“”)
    公共字符串电子邮件
    {
    获取{return\u email;}
    设置{u email=value;}
    }
    私有全局::ProtoBuf.IExtension扩展对象;
    全局::ProtoBuf.IExtension全局::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
    {return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject,createIfMissing);}
    }
    }
    

    请注意,如果您希望it尝试大小写规范化(因此您可以获得
    Id
    Name
    Email
    ,等等),或者希望it包含额外的序列化程序支持(
    BinaryFormatter
    DataContractSerializer
    ,等等)

    umm,不。编写解析代码远没有运行protobuf编译器那么容易。@Ben——谁说过写解析代码的。NET和Boost为您完成所有工作。您不需要解析anything@Ben事实上,“只是使用序列化”这句话让我很开心。。。ToBuf是什么,确切地说?@ ICEMANDIN:Boost可能会做重的提升(XML转换为文档模型),但是你仍然需要编写代码来将标签和属性中的数据拉进C++对象中。这不是小事,protobuf在这方面帮了大忙。更不用说比XML更高效(在空间和计算方面),我想从一个原型文件生成一个类似的文件。我在哪里可以找到protogen工具?
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    // Generated from: test2.proto
    namespace test2
    {
      [global::System.Serializable, global::ProtoBuf.ProtoContract(Name=@"Person")]
      public partial class Person : global::ProtoBuf.IExtensible
      {
        public Person() {}
    
        private int _id;
        [global::ProtoBuf.ProtoMember(1, IsRequired = true, Name=@"id", DataFormat = global::ProtoBuf.DataFormat.TwosComplement)]
        public int id
        {
          get { return _id; }
          set { _id = value; }
        }
        private string _name;
        [global::ProtoBuf.ProtoMember(2, IsRequired = true, Name=@"name", DataFormat = global::ProtoBuf.DataFormat.Default)]
        public string name
        {
          get { return _name; }
          set { _name = value; }
        }
    
        private string _email = "";
        [global::ProtoBuf.ProtoMember(3, IsRequired = false, Name=@"email", DataFormat = global::ProtoBuf.DataFormat.Default)]
        [global::System.ComponentModel.DefaultValue("")]
        public string email
        {
          get { return _email; }
          set { _email = value; }
        }
        private global::ProtoBuf.IExtension extensionObject;
        global::ProtoBuf.IExtension global::ProtoBuf.IExtensible.GetExtensionObject(bool createIfMissing)
          { return global::ProtoBuf.Extensible.GetExtensionObject(ref extensionObject, createIfMissing); }
      }
    
    }