如何将二维数组复制到非托管内存 RT很急,需要完整的C#代码Marshal的copy方法貌似只能复制一维数组另外最后要这个二维数组的内存地址多谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 或者你直接调用API CopyMemoryMarshal应该是可以的...不过我现在没有msdn....上班了再帮你看吧 大概是这样,看看是否在简化一下byte[] vBuffer = new byte[256];int[,] vInts = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };IntPtr source = Marshal.UnsafeAddrOfPinnedArrayElement(vInts, 0);Marshal.Copy(source, vBuffer, 0, 6 * sizeof(int));for (int i = 0; i < 6 * sizeof(int); i++) Console.WriteLine(vBuffer[i]); 请问byte[] vBuffer =newbyte[256];6*sizeof(int)这两部分能给我解释下么?非常感谢 请问byte[] vBuffer = new byte[256];6 * sizeof(int)这两部分能给我解释下么?先把代码再优化一下int[,] vInts = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };byte[] vBuffer = new byte[vInts.Length * sizeof(int)];IntPtr source = Marshal.UnsafeAddrOfPinnedArrayElement(vInts, 0);Marshal.Copy(source, vBuffer, 0, vInts.Length * sizeof(int));for (int i = 0; i < vBuffer.Length; i++) Console.WriteLine(vBuffer[i]);首先我们得计算这个二维数组在内存中占用的空间(单位是byte)vInts.Length * sizeof(int) // 用元素个数乘上每个元素占用的空间其次我们要的到数组的内存起始地址(元素在内存中是顺序存放的)IntPtr source = Marshal.UnsafeAddrOfPinnedArrayElement(vInts, 0);目标地址也一样可以得到IntPtr dest = Marshal.UnsafeAddrOfPinnedArrayElement(vBuufer, 0);IntPtr类型即可和非托管内存转换了。 C# 如何保留指定的有效位数 查看端口是否在使用 如何在TreeView里加入背景图片? string aaa= 用C#编写类似Word的插入特殊符号,急! 关于用C#来写javascript脚本的问题 大家好,请问怎样在HTML语言中,让当前活动的菜单字体变成红色. 输入问题 如何判断机器的端口号被占用,如果占用后怎么样才能将他释放??? c#高手,关于dcom,远程启动encoder遇到的问题?一定送分。 TextBox.Text和线程类属性的绑定 显示一个文件夹中的图片文件的缩略图有控件没有?
Marshal应该是可以的...不过我现在没有msdn....上班了再帮你看吧
byte[] vBuffer = new byte[256];
int[,] vInts = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };
IntPtr source = Marshal.UnsafeAddrOfPinnedArrayElement(vInts, 0);
Marshal.Copy(source, vBuffer, 0, 6 * sizeof(int));
for (int i = 0; i < 6 * sizeof(int); i++)
Console.WriteLine(vBuffer[i]);
6*sizeof(int)这两部分能给我解释下么?非常感谢
6 * sizeof(int)这两部分能给我解释下么?先把代码再优化一下
int[,] vInts = new int[,] { { 1, 2, 3 }, { 4, 5, 6 } };byte[] vBuffer = new byte[vInts.Length * sizeof(int)];
IntPtr source = Marshal.UnsafeAddrOfPinnedArrayElement(vInts, 0);
Marshal.Copy(source, vBuffer, 0, vInts.Length * sizeof(int));
for (int i = 0; i < vBuffer.Length; i++)
Console.WriteLine(vBuffer[i]);首先我们得计算这个二维数组在内存中占用的空间(单位是byte)
vInts.Length * sizeof(int) // 用元素个数乘上每个元素占用的空间其次我们要的到数组的内存起始地址(元素在内存中是顺序存放的)
IntPtr source = Marshal.UnsafeAddrOfPinnedArrayElement(vInts, 0);
目标地址也一样可以得到
IntPtr dest = Marshal.UnsafeAddrOfPinnedArrayElement(vBuufer, 0);
IntPtr类型即可和非托管内存转换了。