C # calls C + + platform invoke p/invoke struct--input and output parameters, return value, rebate value, struct array as parameter "five"

Source: Internet
Author: User

"1" structure as input and output parameters


C + + code:

typedef struct _TESTSTRU1{INTIVAL;CHARCVAL;__INT64 Llval;} TESTSTRU1;
Exportdll_api void Struct_change (testStru1 *pstru) {if (NULL = = Pstru) {return;} Pstru->ival = 1;pstru->cval = ' a ';p stru->llval = 2;wprintf (L "struct_change \ n");}

C # code: specified as ref

/*   1. Mark the structure with StructLayout, specify the structure body storage layout *   2. The order of field definitions  *   3. Field type  *   4. The size of the field in memory  *   5. unmanaged and managed struct names can be different  *///4.1 struct as input/output parameter [StructLayout (LayoutKind.Sequential, CharSet=CharSet.Unicode)]public struct teststru1{public   intival;   Public sbyte    Cval;   public long      llval;}; [DllImport ("ExportDll.dll", CharSet = charset.unicode)]public static extern void Struct_change (ref testStru1 Pstru);

Test:

CEXPORTDLL.TESTSTRU1 stru1 = new Cexportdll.teststru1 (); Cexportdll.struct_change (ref STRU1);


"2" struct as return value


C + + code:

typedef struct _TESTSTRU5{INTIVAL;} Teststru5;teststru5g_stru5;
Exportdll_api teststru5* Struct_return () {g_stru5.ival = 5;wprintf (L "Struct_return \ n"); Return (&G_STRU5);}

C # code, define a return value of IntPtr, and then parse:

[StructLayout (LayoutKind.Sequential, charset=charset.unicode)]public struct teststru5{public    intival;}; [DllImport ("ExportDll.dll", CharSet = charset.unicode)]public static extern IntPtr Struct_return ();

Test:

IntPtr struintptr = Cexportdll.struct_return (); Cexportdll.teststru5 Stru5 = (cexportdll.teststru5) (Marshal.PtrToStructure (Struintptr, typeof (Cexportdll.teststru5 )));

"3" struct array as a parameter


C + + code:

typedef struct _TESTSTRU6{INTIVAL;} TestStru6;
Exportdll_api void Struct_struarr (testStru6 *pstru, int len) {if (NULL = = Pstru) {return;} for (int ix=0; ix<len; ix++) {pstru[ix].ival = IX;} wprintf (L "Struct_struarr \ n");}

C # code: defined as teststru6[], if you need to return the modified value, you need to specify the [in,out] parameter

[StructLayout (LayoutKind.Sequential, charset=charset.unicode)]public struct teststru6{public    intival;}; [DllImport ("ExportDll.dll", CharSet = charset.unicode)]public static extern void Struct_struarr ([In, out]teststru6[] Pstru, int len);

Test:

Cexportdll.teststru6 []STRU6 = new cexportdll.teststru6[5]; Cexportdll.struct_struarr (Stru6, 5);

"4" struct as a rebate parameter, releasing unmanaged memory


C + + code:

typedef struct _TESTSTRU8{INTM;} TestStru8;
Exportdll_api void Struct_parameterout (TestStru8 **ppstru) {if (NULL = = Ppstru) {return;} *ppstru = (teststru8*) cotaskmemalloc (sizeof (TESTSTRU8));(*ppstru)->m= 8;wprintf (L "struct_parameterout \ n");}

C # code: can be defined as ref IntPtr, need to parse

[StructLayout (LayoutKind.Sequential, charset=charset.unicode)]public struct teststru8{public    intm;}; [DllImport ("ExportDll.dll", CharSet = charset.unicode)]public static extern void Struct_parameterout (ref IntPtr Ppstru) ;

Test:

<strong>intptr outptr = IntPtr.Zero; Cexportdll.struct_parameterout (ref outptr); Cexportdll.teststru8 stru8 = (cexportdll.teststru8) (Marshal.PtrToStructure (Outptr, typeof (Cexportdll.teststru8))); Marshal.freecotaskmem (outptr);</strong>


C # calls C + + platform invoke p/invoke struct--input and output parameters, return value, rebate value, struct array as parameter "five"

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.