P/invoke fragmentation--Marshaling of arrays

Source: Internet
Author: User

Because the array is a reference type, the processing of the array is divided into two cases depending on whether the type of the array element is a type that can be passed directly to unmanaged code. The main goal is to see how the memory changes, whether it's copying or locking .

The elements in the array are types that can be passed directly into unmanaged code

This type is many, such as int double.

The completed managed and unmanaged code is as follows:

///////////////////////unmanaged codeextern "C"__declspec (dllexport)voidDoarray (intA[],intlength) {     for(intI=0; i<length;i++) {printf ("%d\n", A[i]); A[i]= About; }}////////////////////////////Managed Code[DllImport (@"C:\Users\Administrator\Desktop\pInvoke\CPPDLL\Debug\CPPDLL.dll")]        Private Static extern voidDoarray (int[]arr,intlength); int[] arr =New int[] {1,2,3,4,5,6,7 }; Doarray (Arr,arr. Length);//Breakpoint
foreach (int a in arr)
{
Console.WriteLine (a);
}
        

Trace procedure: Breakpoints are preceded by the place where the managed code calls the function--doarray. This is to look at the memory of the array as follows,

0x01fabaa4 01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 ......
0x01fabab4 05 00 00 00 06 00 00 00 07 00 00 00 00 00 00 00 ......

You can see 7 numbers in a situation. The address is 0x01fabaa4. Write down this address and continue ...

Come into unmanaged code and find the address of a

As you can see, the address of the array in managed code is the same. Re-execute, print the result:

1 2 3 4 5 6 7 99 99 99 99 99 99 99.

  Conclusion: If the element in the array is a type that can be passed directly to unmanaged code. The default marshaling method is a [in,out] decorated, passing is a pointer that locks memory and then passes the pointer, and the changes to the unmanaged code array are reflected in managed code .

The elements of an array are types that are "not directly passed into unmanaged code"

The array element in managed memory is a char type and is a type that is "not directly passed into unmanaged code." Look at the memory situation.

               Private Static extern voidDoArray2 (Char[] arr,intlength); Static voidMain (string[] args)Char[] carry =New Char[] {'a','b','C'}; DoArray2 (Carry, carry.            Length); foreach(CharCinchcarry) {Console.Write (c+"\ t"); }////////////////////////Unmanaged Codeextern "C"__declspec (dllexport)voidDoArray2 (CharCarry[],intlength) {     for(intI=0; i<length;i++) {printf ("%c\t", Carry[i]); Carry[i]='?'; }}

Breakpoint tracking, broken in DoArray2 (Carry,carry. Length) at the call, the distribution of carry in unmanaged code is as follows:

0x01d4bab8-A-xx-------------XX

Then in the unmanaged code realm, the array carry address is displayed as:

0x004adc70-3d D8 DD 4a xx 3d xx abc...=.? J.=. ".

Obviously, not a piece of address, as a result, modifications are not reflected in managed memory.

Results: A b c a B c.

  conclusion: When an element in an array is a type that is "non-transitive to unmanaged code", the marshaling process is to request a piece of memory in unmanaged memory, copy the data from the managed memory string, and then pass the new memory pointer as a parameter to the function, and the operation of the array of functions is based on the new memory. The results are not returned to managed code. It is also worth saying that when the call is complete, the data in unmanaged memory is not released.

To marshal an array using IntPtr

P/invoke fragmentation--Marshaling of arrays

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.