protobuf-net - 在 C++ 上反序列化的问题边 :(

发布于 2024-08-24 13:00:48 字数 1124 浏览 2 评论 0原文

我在 .NET 应用程序中使用 ProtoBuf-Net 来序列化以下内容:(以 .proto 格式)

message ProtoScreenBuffer {
optional int32 MediaId = 1;
optional bytes Data = 2;
optional bool LastBuffer = 3;
optional int64 StartTime = 4;
optional int64 StopTime = 5;
optional int32 Flags = 6;
optional int32 BufferSubType = 7;
optional int32 BufferType = 8;
optional Guid Guid = 9;
repeated int32 EncryptedDataStart = 10;
repeated int32 EncryptedDataLength = 11;
}

我的目标是将其序列化并将其作为单个示例注入到 ASF 文件中。

我将其称为序列化:

MemoryStream ms = new MemoryStream();
Serializer.Serialize<ProtoScreenBuffer>(ms, ProtoScreenBuffer.CreateProtoScreenBuffer (buffer));

然后我从 ms 对象中获取一个字节数组:

ms.ToArray();

并将该字节数组放入 ASF 中。最大的问题是我的 C++ 应用程序可以很好地读取 ASF 示例,当我尝试反序列化它时,我遇到了内存访问冲突:(

这是我的 C++ 代码:(

m_screenBuffer.ParseFromArray(serBuffer, dwInputDataLen); 

其中 m_screenBuffer 是 ProtoScreenBuffer,serBuffer 是我得到的原始字节数组来自 ASF 文件,dwInputDataLen 是它的长度。)

对于我正在尝试做的事情(在 C# .NET 中序列化并在 C++ 中反序列化?),我在这里做的事情是否错误?

非常感谢

Roey 。

I am using ProtoBuf-Net in my .NET application to serialize the following : (in .proto format)

message ProtoScreenBuffer {
optional int32 MediaId = 1;
optional bytes Data = 2;
optional bool LastBuffer = 3;
optional int64 StartTime = 4;
optional int64 StopTime = 5;
optional int32 Flags = 6;
optional int32 BufferSubType = 7;
optional int32 BufferType = 8;
optional Guid Guid = 9;
repeated int32 EncryptedDataStart = 10;
repeated int32 EncryptedDataLength = 11;
}

My aim is to serialize this and inject it into an ASF file, as a single sample.

I call this to serialize :

MemoryStream ms = new MemoryStream();
Serializer.Serialize<ProtoScreenBuffer>(ms, ProtoScreenBuffer.CreateProtoScreenBuffer (buffer));

then I get a byte array from the ms object :

ms.ToArray();

and I put this byte array in the ASF. The big problem is on my C++ app which reads the ASF sample just fine, I get a memory access violation when I try to deserialize it :(

this is my C++ code :

m_screenBuffer.ParseFromArray(serBuffer, dwInputDataLen); 

(where m_screenBuffer is ProtoScreenBuffer, serBuffer is the raw byte array I got from the ASF file, and dwInputDataLen is the length of it.)

Are any of the things I'm doing here wrong , for what I'm trying to do (serialize in C# .NET and deserialize in C++?)

Thanks alot.

Roey

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑饮青盏花 2024-08-31 13:00:48

嗯...我可能认为其中唯一混乱的是 Guid (我最近意识到我对此的编码似乎是相当疯狂的字节序)。所以我认为应该可以正常工作,给出或采取一些混乱的代码来破译Guid

为了排除编码错误,我的建议是:

  • 通过 C# 将数据序列化到文件(或者只是查看调试器中屏幕上的字节)
  • 通过 C++ 将 /same/ 数据序列化到文件(或只需查看调试器中屏幕上的字节)

然后:

  • 比较字节
  • 检查长度是否是您期望的(即您传递的数字)

这应该表明它是否是编码,而不是与传递错误有关内存地址或类似地址。

另外 - 检查您没有使用 GetBuffer() (或者至少,如果您确实使用 GetBuffer(),请确保您使用来自 MemoryStream.Length,而不是来自超大的 byte[])。

Hmm... the only thing in there that I might expect to be messy is the Guid (I recently realised that my encoding of this appears to be fairly crazy-endian). So I think that should work fine, give or take some messy code to decipher the Guid.

To rule out an encoding error, what I would suggest is:

  • serialize the data via C#, to a file (or just look at the bytes on-screen in the debugger)
  • serialize the /same/ data via C++, to a file (or just look at the bytes on-screen in the debugger)

Then:

  • compare the bytes
  • check the length is what you expect (i.e. the number you are passing over)

That should indicate whether it is the encoding, vs something to do with passing the wrong memory address or similar.

Also - check you aren't using GetBuffer() (or at least, if you do use GetBuffer(), make sure you use the .Length from the MemoryStream, and not from the oversized byte[]).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文