码迷,mamicode.com
首页 > Windows程序 > 详细

c#中Marshal.Copy()方法的使用

时间:2014-10-18 16:40:00      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:style   color   ar   使用   strong   sp   数据   on   ad   

c#中Marshal.Copy方法的使用

Marshal.copy()方法用来在托管对象(数组)和非托管对象(IntPtr)之间进行内容的复制

函数有很多重载,如下所示:

Copy(array<Byte>[]()[], Int32, IntPtr, Int32) 将一维的托管 8 位无符号整数数组中的数据复制到非托管内存指针。

Copy(array<Char>[]()[], Int32, IntPtr, Int32) 将数据从一维的托管字符数组复制到非托管内存指针。

Copy(array<Double>[]()[], Int32, IntPtr, Int32) 将数据从一维的托管双精度浮点数组复制到非托管内存指针。

Copy(array<Int16>[]()[], Int32, IntPtr, Int32) 将数据从一维的托管 16 位带符号整数数组复制到非托管内存指针。

Copy(array<Int32>[]()[], Int32, IntPtr, Int32) 将数据从一维的托管 32 位带符号整数数组复制到非托管内存指针。

Copy(array<Int64>[]()[], Int32, IntPtr, Int32) 将数据从一维的托管 64 位带符号整数数组复制到非托管内存指针。

Copy(IntPtr, array<Byte>[]()[], Int32, Int32) 将数据从非托管内存指针复制到托管 8 位无符号整数数组。

Copy(IntPtr, array<Char>[]()[], Int32, Int32) 将数据从非托管内存指针复制到托管字符数组。

Copy(IntPtr, array<Double>[]()[], Int32, Int32) 将数据从非托管内存指针复制到托管双精度浮点数组。

Copy(IntPtr, array<Int16>[]()[], Int32, Int32) 将数据从非托管内存指针复制到托管 16 位带符号整数数组。

Copy(IntPtr, array<Int32>[]()[], Int32, Int32) 将数据从非托管内存指针复制到托管 32 位带符号整数数组。

Copy(IntPtr, array<Int64>[]()[], Int32, Int32) 将数据从非托管内存指针复制到托管 64 位带符号整数数组。

Copy(IntPtr, array<IntPtr>[]()[], Int32, Int32) 将数据从非托管内存指针复制到托管 IntPtr 数组。

Copy(IntPtr, array<Single>[]()[], Int32, Int32) 将数据从非托管内存指针复制到托管单精度浮点数组。

Copy(array<IntPtr>[]()[], Int32, IntPtr, Int32) 将数据从一维托管 IntPtr 数组复制到非托管内存指针。

Copy(array<Single>[]()[], Int32, IntPtr, Int32) 将数据从一维的托管单精度浮点数组复制到非托管内存指针。

这里需要注意的是两个参数Int32类型的使用

Int32类型的两个参数都是用来限定数组的,其中一个限定开始位置,一个限定长度

"xuwei";  

  • IntPtr pName = Marshal.AllocHGlobal(2*name.Length);  
  • Marshal.Copy(name.ToCharArray(), 0, pName, name.Length);  
  • char[] cName = new char[name.Length];  
  • Marshal.Copy(pName, cName, 0, name.Length);  

易知name.Length=5

(1) 给pName指针分配了2*name.Length字节的空间

注意:Marshal.AllocHGlobal(Int32 cb)中的参数cb是分配的字节数

(2) 将name转换的char[]中的内容复制到pName所指的内存中,所取长度为char的个数,即name.Length

(3) 给cName分配name.Length个char位置

(4) 将pName中的内容复制到cName数组中,长度同样为name.Length

c#中Marshal.Copy()方法的使用

标签:style   color   ar   使用   strong   sp   数据   on   ad   

原文地址:http://www.cnblogs.com/zhaoxinshanwei/p/4033200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!