Use of the Marshal.Copy () method in C #

Source: Internet
Author: User

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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.