use of the Marshal.Copy method in C #
The Marshal.Copy () method is used to replicate content between managed objects (arrays) and unmanaged objects (INTPTR)
The function has many overloads, as follows:
Copy (array<byte>[] () [], Int32, INTPTR, Int32) copies data from a one-dimensional, managed 8-bit unsigned integer array to an unmanaged memory pointer.
Copy (array<char>[] () [], Int32, INTPTR, Int32) copies data from a one-dimensional, managed character array to an unmanaged memory pointer.
Copy (array<double>[] () [], Int32, INTPTR, Int32) copies data from a one-dimensional, managed double-precision floating-point array to an unmanaged memory pointer.
Copy (array<int16>[] () [], Int32, INTPTR, Int32) copies data from a one-dimensional, managed 16-bit signed integer array to an unmanaged memory pointer.
Copy (array<int32>[] () [], Int32, INTPTR, Int32) copies data from a one-dimensional, managed 32-bit signed integer array to an unmanaged memory pointer.
Copy (array<int64>[] () [], Int32, INTPTR, Int32) copies data from a one-dimensional, managed 64-bit signed integer array to an unmanaged memory pointer.
Copy (INTPTR, array<byte>[] () [], Int32, Int32) copies data from an unmanaged memory pointer to an array of managed 8-bit unsigned integers.
Copy (INTPTR, array<char>[] () [], Int32, Int32) copies data from an unmanaged memory pointer to a managed character array.
Copy (INTPTR, array<double>[] () [], Int32, Int32) copies data from an unmanaged memory pointer to a managed double-precision floating-point array.
Copy (INTPTR, array<int16>[] () [], Int32, Int32) copies data from an unmanaged memory pointer to a managed 16-bit signed integer array.
Copy (INTPTR, array<int32>[] () [], Int32, Int32) copies data from an unmanaged memory pointer to a managed 32-bit signed integer array.
Copy (INTPTR, array<int64>[] () [], Int32, Int32) copies data from an unmanaged memory pointer to a managed 64-bit signed integer array.
Copy (INTPTR, array<intptr>[] () [], Int32, Int32) copies data from an unmanaged memory pointer to a managed INTPTR array.
Copy (INTPTR, array<single>[] () [], Int32, Int32) copies data from an unmanaged memory pointer to a managed single-precision floating-point array.
Copy (array<intptr>[] () [], Int32, INTPTR, Int32) copies data from a one-dimensional managed IntPtr array to an unmanaged memory pointer.
Copy (array<single>[] () [], Int32, INTPTR, Int32) copies data from a one-dimensional, managed single-precision floating-point array to an unmanaged memory pointer.
It is important to note that the use of the two parameter Int32 type
Thetwo parameters of the I nt32 type are used to qualify the array, one of which is a qualified starting position, a limited length
"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);
Easy to know name. Length=5
(1) Assigning 2*name to the pname pointer . Length bytes of space
NOTE: The parameter CB inMarshal.allochglobal (Int32 CB) is the number of bytes allocated
(2) Copy the contents of the name conversion char[] to the memory referred to by pname, and the length is the number of char, that is, name. Length
(3) assigning name.length char locations to a CNAME
(4) Copy the contents of the pname into the CNAME array with the same length as name. Length
Use of the Marshal.Copy () method in C #