Study-Computer2008.03.20 06:11

Reference

 
 

/*************************************************************** * C Function Scalars * ***************************************************************/ // C Function Scalars __declspec(dllexport) void __stdcall

Scalars(double in, double *out) { *out = in; } // C Function OneDArray __declspec(dllexport) void __stdcall

OneDArray(double anArray[], int n) { anArray[0] = 0; } // C Function TwoDArray #define A(I,J) a2dArray[I*m+J] __declspec(dllexport) void __stdcall

TwoDArray(double a2dArray[], int m, int n) { a2dArray[0*n+0] = 0; a2dArray[1*n+0] = 0; a2dArray[0*n+1] = 0; } // C Function TryComplex struct Complex { double re; double im; }; __declspec(dllexport) void __stdcall

TryComplex(Complex inputVar, Complex *outputVar, Complex array[], int n) { outputVar->re = ++inputVar.re; outputVar->im = ++inputVar.im; array[0].re = 99.0; array[0].im = 98.0; array[1].re = 97.0; array[1].im = 96.0; } // C Function MarshalStructC struct marshalStruct { int array_length; double *array; }; __declspec(dllexport) void __stdcall

MarshalStructC(marshalStruct *pointerinStruct) { int i; pointerinStruct->array = (double *)malloc(

(size_t)(sizeof(double)*pointerinStruct->array_length)); for (i = 0; i <pointerinStruct->array_length; i++) { pointerinStruct->array[i] = (double)(i) + 1.0; } } __declspec(dllexport) void __stdcall

FreeMarshalStructptr(marshalStruct *pointerinStruct) { free(pointerinStruct->array); pointerinStruct->array = 0; } // C Function CallBack // C Function CallbackWithStruct // C Function EnumString // // reference: http://www.ddj.com/cpp/184406285 /*************************************************************** * C# Class * ***************************************************************/ using System; using System.Runtime.InteropServices; namespace DDJexamples { [StructLayout(LayoutKind.Sequential)] public struct Complex { public double re; public double im; }; [StructLayout(LayoutKind.Sequential)] public struct marshalStruct { public int array_length; public IntPtr array; }; public class ExerciseScalars { [DllImport("cmarshaldll")] public static extern void

Scalars(double invar, ref double outvar); [DllImport("cmarshaldll")] public static extern void

OneDArray(double [] anArray, int n); [DllImport("cmarshaldll")] public static extern void

TwoDArray(double [,] a2dArray, int m, int n); [DllImport("cmarshaldll")] public static extern void

TryComplex(Complex inputVar, ref Complex outputVar,

[In, Out] Complex [] array, int n); [DllImport("cmarshaldll")] public static extern void

MarshalStructC(ref marshalStruct pointerinStruct); [DllImport("cmarshaldll")] public static extern void

FreeMarshalStructptr(ref marshalStruct pointerinStruct); public static void Main() { double invar = 5.0; double outvar = 0.0; Scalars(invar, ref outvar); double [] anArray = new double [2]; OneDArray(anArray, anArray.GetLength(0)); double [,] a2dArray = new double [3,2]; TwoDArray(a2dArray, a2dArray.GetLength(0),

a2dArray.GetLength(1)); Complex inputVar = new Complex(); Complex outputVar = new Complex(); Complex [] array = new Complex[n]; inputVar.re = 1.0; inputVar.im = 2.0; TryComplex(inputVar, ref outputVar,

array, array.GetLength(0)); marshalStruct pointerinStruct = new marshalStruct() ; pointerinStruct.array_length = 5; MarshalStructC(ref pointerinStruct); double [] x = new double[pointerinStruct.array_length]; Marshal.Copy( pointerinStruct.array, x, 0,

pointerinStruct.array_length ); MarshalStructptr(ref pointerinStruct); } } }

Posted by htna