C# - Marshal.Copy() 导致缓冲区损坏
我收到一个 IntPtr 和一个 int 指定它指向的字节数。数据可以包含任何字符,包括 null、EOL 等。尝试以下操作时,缓冲区已损坏:
//buffer is the IntPtr
//count is the number of bytes in 'buffer'
byte[] test = new byte[count];
Marshal.Copy(buffer, test, 0, count);
IntPtr ptr = IntPtr.Zero;
ptr = Marshal.AllocCoTaskMem(count);
Marshal.Copy(test, 0, ptr, count);
我假设“buffer”和“ptr”将指向不同内存位置中的相同缓冲区 blob,但它们不会(我只是将相同的数据复制到另一个内存位置 AFAIK)。然而,“ptr”似乎指向任意内存位置,因为它包含对 DLL 模块的字符串引用。
有什么想法吗?谢谢!
I am receiving an IntPtr and an int specifying the number of bytes it points to. The data can contain any characters including null, EOL, etc. When trying the following, the buffer is corrupted:
//buffer is the IntPtr
//count is the number of bytes in 'buffer'
byte[] test = new byte[count];
Marshal.Copy(buffer, test, 0, count);
IntPtr ptr = IntPtr.Zero;
ptr = Marshal.AllocCoTaskMem(count);
Marshal.Copy(test, 0, ptr, count);
I would assume 'buffer' and 'ptr' would be pointing to the same buffer blob in different memory locations but they don't (I am just copying the same data to another mem location AFAIK). However, 'ptr' seems to point to an arbitrary memory location as it contains string references to DLL modules.
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论