A one-dimensional array with built-in data types in the "1" structure
C + + code:
typedef struct _TESTSTRU3{INTIVALARRP[30]; WCHARSZCHARR[30];} TESTSTRU3;
Exportdll_api void Struct_changearr (TestStru3 *pstru) {if (NULL = = Pstru) {return;} Pstru->ivalarrp[0] = 8;LSTRCPYNW (Pstru->szcharr, L "as", +) wprintf (l "struct_changearr \ n");}
C # code: Define an array and specify its length through SizeConst (for character arrays, you can specify string directly)
[StructLayout (LayoutKind.Sequential, charset=charset.unicode)]public struct teststru3{ [MarshalAs ( UnmanagedType.ByValArray, sizeconst=30)] public INT[]IVALARRP; [MarshalAs (UnmanagedType.ByValTStr, SizeConst =)] public string Szcharr;}; [DllImport ("ExportDll.dll", CharSet = charset.unicode)]public static extern void Struct_changearr (ref testStru3 Pstru);
Test:
Cexportdll.teststru3 stru3 = new Cexportdll.teststru3 (); Cexportdll.struct_changearr (ref STRU3);
a two-dimensional array with built-in data types in the "2" Structure
C + + code:
typedef struct _TESTSTRU7{INTM[5][5];} TestStru7;
Exportdll_api void Struct_change2darr (TestStru7 *pstru) {if (NULL = = Pstru) {return;} PSTRU->M[3][3] = 1;wprintf (L "Struct_change2darr \ n");}
C # code: splitting a two-dimensional array into two one-dimensional arrays
[StructLayout (LayoutKind.Sequential, charset=charset.unicode)]public struct teststru7pre{ [MarshalAs ( UnmanagedType.ByValArray, sizeconst=5)] public int[]m;}; [StructLayout (layoutkind.sequential, CharSet = charset.unicode)]public struct teststru7{ [MarshalAs ( UnmanagedType.ByValArray, SizeConst = 5)] public teststru7pre []m;}; [DllImport ("ExportDll.dll", CharSet = charset.unicode)]public static extern void Struct_change2darr (ref testStru7 Pstru );
Test:
Cexportdll.teststru7 stru7 = new Cexportdll.teststru7 (); Cexportdll.struct_change2darr (ref STRU7);
the "3" struct contains a string pointer
C + + code:
typedef struct _TESTSTRU9{WCHAR*PWCHARR; Char*pcharr;booliscbool; Boolisbool;} TestStru9;
Exportdll_api void Struct_changeptr (TestStru9 *pstru) {if (NULL = = Pstru) {return;} Pstru->isbool = True;pstru->isbool = True;pstru->pwcharr = (wchar*) cotaskmemalloc (8*sizeof (WCHAR));p stru- >pcharr = (char*) cotaskmemalloc (8*sizeof (CHAR)), LSTRCPYNW (Pstru->pwcharr, L "Ghj", 8); Lstrcpyna (pStru-> Pcharr, "Ghj", 8); wprintf (L "struct_changeptr \ n");}
C # code, defined as a string, note the difference between BOOL and bool :
[StructLayout (LayoutKind.Sequential, charset=charset.unicode)]public struct teststru9{ [MarshalAs ( UNMANAGEDTYPE.LPWSTR)] public string Pwcharr; [MarshalAs (UNMANAGEDTYPE.LPSTR)] public string Pcharr; [MarshalAs (UNMANAGEDTYPE.U1)] public bool Iscbool; [MarshalAs (Unmanagedtype.bool)] public bool Isbool;}; [DllImport ("ExportDll.dll", CharSet = charset.unicode)]public static extern void Struct_changeptr (ref testStru9 Pstru);
Test:
<strong>cexportdll.teststru9 stru9 = new Cexportdll.teststru9 (); Cexportdll.struct_changeptr (ref stru9);</strong>
C # calls the C + + platform invoke P/invoke struct-a one-dimensional, two-dimensional array with built-in data types, and a string pointer of "six"