1. If the function has only passed in parameters, such as: output function int __declspec (dllexport) test (const int N) {return n+10 in C + + code Copy code to Clipboard//c++; }
The corresponding C # codes are: C # code Copy code to Clipboard [DllImport ("Test.dll", EntryPoint = "#1")] the public static extern int test (int m); private void Button1_Click (object sender, EventArgs e) {textbox1.text= test (10). ToString (); }
2. If the function has outgoing parameters, such as: C + + code Copy code to Clipboard//c++ void __declspec (dllexport) test (const int N, int& Z) {z=n+10 ; }
Corresponding C # codes: C # code Copy code to Clipboard [DllImport ("Test.dll", EntryPoint = "#1")] public static extern double test (int m, ref int n); private void Button1_Click (object sender, EventArgs e) {int n = 0; Test1 (ref N); textbox1.text= n.tostring ();}
3. With incoming array: c/n + + code Copy code to Clipboard void __declspec (dllexport) test (const int N, const int n[], int& Z) {for (int i=0; i<n; i++) {z+=n[i];} }
C # codes: C # code Copy code to Clipboard [DllImport (' test.dll ', entrypoint = ' #1 ')] public static extern double test (int N, T[] n, ref int Z); private void Button1_Click (object sender, EventArgs e) {int N = 0; int[] n; n = new int[10]; for (int i = 0; i < i + +) {N[i] = i;} test (N.length, n, ref N); textbox1.text= n.tostring (); }
4. With an outgoing array:
C + + cannot directly outgoing an array, only outgoing array pointers, C + + code Copy code to Clipboard void __declspec (dllexport) test (const int M, const int n[], int *n) {for (int i=0; i<m; i++) {n[i]=n[i]+10;}}
Corresponding C # codes: C # code Copy code to Clipboard [DllImport ("Test.dll", EntryPoint = "#1")] public static extern void Test (int N, I Nt[] n, [MarshalAs (Unmanagedtype.lparray,sizeparamindex=1)] int[] Z); private void Button1_Click (object sender, EventArgs e) {int N = 1000; int[] n, Z; n = new Int[n]; Z = new Int[n]; for (int i = 0; i < n; i++) {n[i] = i;} test (N.length, N, Z); for (int i=0; i<z.length; i++) {textbox1.appendtext (z[i). ToString () + "n"); } }
When declaring a function entry, note that this sentence [MarshalAs (unmanagedtype.lparray,sizeparamindex=1)] int[] Z
The array is used directly in C #, and the pointer to the array is returned in C + +, which is used to transform the two different types.
For the MarshalAs parameter usage and the marshaling of the array, see this article reprint: http://www.kycis.com/blog/read.php?21
5. Outgoing character array:
C/C + + definition: + + code Copy code to Clipboard void __declspec (dllexport) test (int i, double &a, double &b, Char t[5])
C # corresponding declaration: C # code Copy code to Clipboard [DllImport ("Dll.dll", EntryPoint = "test")] public static extern void Test (int i, Ref double A, ref double b, [out, MarshalAs (UnmanagedType.LPArray)] char[] t); 。。。 char[] t = new char[5]; Test (I, ref a, ref B, t);
The passing of a character array is basically similar to 4, except that the Mashalas is preceded by an out.
Http://www.kycis.com/blog/read.php?20