Marshal avec un tableau à deux dimensions avec une DLL native en C
Bonjour,
Dans le cadre d'un projet je dois utiliser une DLL native en C avec le C# 4.0. J'ai fait l'export de plusieurs méthodes sans difficulté. En revanche, j'ai un problème à marshaliser un tableau à deux dimensions pour certaines d'entre elle.
Voici le code en C de la fonction
Code:
1 2 3 4 5 6 7 8 9
| __declspec (dllexport) extern void fisPrintMatrix(DOUBLE **matrix, int row_n, int col_n)
{
int i, j;
for (i = 0; i < row_n; i++) {
for (j = 0; j < col_n; j++)
PRINTF("%.3f ", matrix[i][j]);
PRINTF("\n");
}
} |
Voici le code C# pour l'utiliser
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| [DllImport( "fis.dll", EntryPoint="fisPrintMatrix" )]
static extern void fisPrintMatrix(IntPtr matrix, int row_n, int col_n);
static void Main( string[] args )
{
double[][] test_matrix = { new double[] {1.1,2.2},
new double[] {3.3,4.4},
new double[] {5.5,6.6}};
IntPtr pa1 = marshalJaggedArray( test_matrix );
Console.WriteLine( " Les valeurs de la matrice sont : ");
fisPrintMatrix( pa1, 3, 2 );
}
static private IntPtr marshalJaggedArray( double[][] array )
{
int sizeofPtr = Marshal.SizeOf( typeof( IntPtr ) );
int sizeofDouble = Marshal.SizeOf( typeof( double ) );
IntPtr p1 = Marshal.AllocCoTaskMem( array.Length * sizeofPtr );
for ( int i = 0 ; i < array.Length ; i++ )
{
IntPtr v1 = Marshal.AllocCoTaskMem( array[i].Length * sizeofDouble );
Marshal.Copy( array[i], 0, v1, array[i].Length );
Marshal.WriteIntPtr( p1, i * sizeofPtr, v1 );
}
return p1;
} |
À l’exécution de la ligne : fisPrintMatrix( pa1, 3, 2 ) j'ai ce message d'erreur :
Citation:
A call to PInvoke function 'Fuzzy_logic_toolbox!Fuzzy_logic_toolbox.Lire_fichier_fis::fisPrintMatrix' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
J'ai quelques difficultés à trouver une solution. Si vous avez des pistes, je suis preneur. Je vous remercie par avance :)