DELPHI PROTOBUF免费的开源支持库fundamentals5

DELPHI PROTOBUF免费的开源支持库fundamentals5

1、源码URL:

https://github.com/fundamentalslib/fundamentals5

2、编译ProtoCodeGen.exe
下载完上面的文件后,在子目录中找到 ProtocolBuffersCodeGenAppProtoCodeGen.dpr  ----- 编译它

3、编译完毕后,得到ProtoCodeGen.exe一个。它就可以把   *.proto  文件,生成为PAS文件了。

4、在D盘下,我新建了一个Google文件夹,把刚刚生成的protocodegen.exe放了进去

在这个文件夹下,我用记事本新建了一个文件,里面的内容如下

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

保存为   message.proto   

同样,我在Google文件夹下用建立一个叫做 pas_out文件夹

具体编译的指令时这样的。

打开 CMD。(DOS命令行那个,在开始——》运行——》CMD——》回车)

敲入命令

这个时候,去D:googlepas_out下去找,会发现生成了一个pbMessageMessages.pas文件。

大功告成!!!!!

5、如何使用

新建一个项目

把Fundamentals.ProtoBuf.4.0.01(加压文件夹中的) ProtocolBuffers和Utils目录下的文件拷贝到我们新建的项目下,添加到项目里。

恩,其实并不是都能用的上的。这个自己看生成的pbMessageMessages.pas其他它就需要3个引用

  cUtils,
  cStrings,
  cProtoBufUtils;

加密或者解密(序列化或者反序列化的代码如下)

var p1,p2:TPersonRecord;
  len:Integer;
  Bt:TBytes;
begin
  p1.Name := edtName.Text;
  p1.Id := StrToInt(edtID.Text);
  SetLength(Bt,100);
  //加密
  //len := pbEncodeValuePersonRecord(Bt[0],100,p1);
  len := pbEncodeValuePersonRecord(Bt[0],100,p1);
  SetLength(Bt,len);
  mmo1.Lines.Add(IntToStr(len));
  //解密
  //PersonRecordInit(p2);
  len :=pbDecodeValuePersonRecord(Bt[0],len,p2);
  mmo1.Lines.Add(IntToStr(len));
  mmo1.Lines.Add(p2.Name);
end;

彻底大功告成!!!!!

原文地址:https://www.cnblogs.com/hnxxcxg/p/9405337.html