为什么Marshal Copy在C ++ / CLI中的未知字符后停止? [英] Why does Marshal Copy Stop After Unknown Character in C++/CLI?

查看:92
本文介绍了为什么Marshal Copy在C ++ / CLI中的未知字符后停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是错误的部分我很抱歉;我不熟悉这些论坛的布局。



我试图将托管数组复制到非托管字符数组中。仔细观察每个MyCharArray和ReadArray的内容,我执行了以下代码: 


   char MyCharArray [100];

  阵列<系统::字节> ^ ReadArray = gcnew数组< System :: Byte>(73);

   DataStream-> Read(ReadArray,0,pIx-> MsgLen);

  System :: Runtime :: InteropServices :: Marshal :: Copy(ReadArray,0,System :: IntPtr(MyCharArray),72)。




的副本之前,MyCharArray是"ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"&NBSP;&NBSP;
  char *

"字符*

中的副本后,MyCharArray是

"¯¾ÞÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"&NBSP;&NBSP;   char *



复制之前和之后,ReadArray的内容如下:

175'¯'; 190'¾'; 173' - '; 222'Þ'; 13''; 208'Ð'; 13' - '; 240'ð'; 1''; 0''; 0''; 0'';


依此类推。很明显,数据在达到位置4的值13后停止复制,但我不知道为什么,也不知道如何防止这种行为。如果这种行为是不可避免的,我可以使用哪些替代方法?感谢
您可以提供的任何帮助。

解决方案

阵列<系统::字节> ^ ReadArray = gcnew array< System :: Byte>(73){175,190,173,222,13,208,13,240,1,0,0,0};
char MyCharArray [100];
System :: Runtime :: InteropServices :: Marshal :: Copy(ReadArray,0,System :: IntPtr(MyCharArray),72);




你能提供更多细节来重现这个问题吗? / p>


My apologies if this is in the wrong section; I'm unfamiliar with the layout of these forums.

I am attempting to copy a managed array into an unmanaged character array. Putting a watch on the contents of each of MyCharArray and ReadArray, I executed the following code:  
   char MyCharArray[100];
   array<System::Byte> ^ReadArray = gcnew array<System::Byte>(73);
   DataStream->Read(ReadArray, 0, pIx->MsgLen);
  System::Runtime::InteropServices::Marshal::Copy ( ReadArray, 0, System::IntPtr(MyCharArray), 72).

Before the copy, MyCharArray is "ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"    char*
" char*
After the copy, MyCharArray is
"¯¾­ÞÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"    char*

Both before and after the copy, the contents of ReadArray is as follows:
175 '¯'; 190 '¾'; 173 '-­'; 222 'Þ'; 13 ''; 208 'Ð'; 13 '-'; 240 'ð'; 1 ''; 0 ''; 0 ''; 0 '';

And so on. It's clear that the data ceases copying after reaching the value 13 at position 4, but I do not know why, nor how to prevent this behavior. If this behavior is unavoidable, which alternate methods are available that I could use? Thank you for any help you could provide.

解决方案

Seems to work using .NET Framework 3.5 in Visual Studio 2017:

array<System::Byte> ^ReadArray = gcnew array<System::Byte>( 73 ) { 175, 190, 173, 222, 13, 208, 13, 240, 1, 0, 0, 0 };
char MyCharArray[100];
System::Runtime::InteropServices::Marshal::Copy( ReadArray, 0, System::IntPtr( MyCharArray ), 72 );


Can you give more details to reproduce the problem?


这篇关于为什么Marshal Copy在C ++ / CLI中的未知字符后停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆