Use of Marshal. Copy () method in C #

Source: Internet
Author: User

Use of the marshal. COPY method in C #

The marshal. Copy () method is used to copy content between hosted objects (arrays) and non-hosted objects (intptr ).

There are many overload functions, as shown below:

Copy (array <byte> [] () [], int32, intptr, int32) copies the data in the one-dimensional hosted 8-bit unsigned integer array to the unmanaged memory pointer.

Copy (array <char> [] () [], int32, intptr, int32) Copies data from a one-dimensional hosted character array to an unmanaged memory pointer.

Copy (array <double> [] () [], int32, intptr, int32) Copies data from one-dimensional hosted double-precision floating point groups to unmanaged memory pointers.

Copy (array <int16> [] () [], int32, intptr, int32) Copies data from a one-dimensional hosted 16-bit signed integer array to an unmanaged memory pointer.

Copy (array <int32> [] () [], int32, intptr, int32) Copies data from a one-dimensional hosted 32-bit signed integer array to an unmanaged memory pointer.

Copy (array <int64> [] () [], int32, intptr, int32) Copies data from a one-dimensional hosted 64-bit signed integer array to an unmanaged memory pointer.

Copy (intptr, array <byte> [] () [], int32, int32) Copies data from an unmanaged memory pointer to a hosted 8-bit unsigned integer array.

Copy (intptr, array <char> [] () [], int32, int32) Copies data from an unmanaged memory pointer to a hosted character array.

Copy (intptr, array <double> [] () [], int32, int32) Copies data from an unmanaged memory pointer to a hosted double-precision floating point group.

Copy (intptr, array <int16> [] () [], int32, int32) Copies data from an unmanaged memory pointer to a hosted 16-bit signed integer array.

Copy (intptr, array <int32> [] () [], int32, int32) Copies data from an unmanaged memory pointer to a hosted 32-bit signed integer array.

Copy (intptr, array <int64> [] () [], int32, int32) Copies data from an unmanaged memory pointer to a hosted 64-bit signed integer array.

Copy (intptr, array <intptr> [] () [], int32, int32) Copies data from the unmanaged memory pointer to the managed intptr array.

Copy (intptr, array <single> [] () [], int32, int32) Copies data from an unmanaged memory pointer to a hosted Single-precision floating point group.

Copy (array <intptr> [] () [], int32, intptr, int32) Copies data from a one-dimensional hosted intptr array to an unmanaged memory pointer.

Copy (array <single> [] () [], int32, intptr, int32) Copies data from a one-dimensional hosted Single-precision floating point group to an unmanaged memory pointer.

Note the use of the int32 type of the two parameters.

Both parameters of the int32 type are used to limit the array. One parameter specifies the start position and the other parameter specifies the length.

Note: The length is the number of elements in the index group, not the number of bytes.

Example:

Classic

            string name = "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);

Yi Zhi name. Length = 5

(1) assigned to the pname pointer2 * name. Length Byte Space

Note:Marshal. allochglobal (int32 CB)The CB parameter in is the number of allocated bytes.

(2) copy the content in char [] converted by name to the memory referred to by pname,LengthIs the number of char, that isName. Length

(3) assign name. Length char locations to cname

(4) copy the content in pname to the cname array. The length is also name. length.


Related Article

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.