protobuf-net - 在 C++ 上反序列化的问题边 :(
我在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯...我可能认为其中唯一混乱的是
Guid
(我最近意识到我对此的编码似乎是相当疯狂的字节序)。所以我认为应该可以正常工作,给出或采取一些混乱的代码来破译Guid
。为了排除编码错误,我的建议是:
然后:
这应该表明它是否是编码,而不是与传递错误有关内存地址或类似地址。
另外 - 检查您没有使用
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 theGuid
.To rule out an encoding error, what I would suggest is:
Then:
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 useGetBuffer()
, make sure you use the.Length
from theMemoryStream
, and not from the oversizedbyte[]
).