D
Daniel Platz
Hello,
I would like to pass a two dimensional array to C function in a dll. I
use ctypes to call the function.
I compile the dll with visual studio 2008 express and my C source code
looks like this.
#include <stdio.h>
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
using namespace std;
#endif
__declspec(dllexport) int print(double** ptr, int ny, int nx)
{
int i, j;
for(i=0; i<ny; i++)
{
for(j=0; j<nx; j++)
{
printf("%.3f \t", *(*(ptr+i)+j));
}
printf("\n");
}
return 0;
}
#ifdef __cplusplus
}
#endif
As you can see the function expects a doube** variable. I tried to
call this function from Python with this code.
import ctypes as ct
matrix = ct.cdll.LoadLibrary('matrix.dll')
getattr(matrix, 'print_matrix')
ptr_type = ct.c_double * 5 * 4
ptr = ptr_type()
matrix.print_matrix(ptr, ct.c_int(4), ct.c_int(5))
I expected to see a matrix showing only zeros since I can address the
single entries in Python by ptr[j]. Instead the interpreter returns
with the error message
WindowsError: exception: access violation reading 0x000000
WARNING: Failure executing file: <ctypes_matrix.py>
Furthermore, I am wondering if there is a fast way to use a numpy 2D
array instead or alternatively to cast the ctypes array into a numpy
array.
Has someone an idea to help me?
Thank you very much
Daniel
I would like to pass a two dimensional array to C function in a dll. I
use ctypes to call the function.
I compile the dll with visual studio 2008 express and my C source code
looks like this.
#include <stdio.h>
#ifdef __cplusplus
extern "C" { // only need to export C interface if
// used by C++ source code
using namespace std;
#endif
__declspec(dllexport) int print(double** ptr, int ny, int nx)
{
int i, j;
for(i=0; i<ny; i++)
{
for(j=0; j<nx; j++)
{
printf("%.3f \t", *(*(ptr+i)+j));
}
printf("\n");
}
return 0;
}
#ifdef __cplusplus
}
#endif
As you can see the function expects a doube** variable. I tried to
call this function from Python with this code.
import ctypes as ct
matrix = ct.cdll.LoadLibrary('matrix.dll')
getattr(matrix, 'print_matrix')
ptr_type = ct.c_double * 5 * 4
ptr = ptr_type()
matrix.print_matrix(ptr, ct.c_int(4), ct.c_int(5))
I expected to see a matrix showing only zeros since I can address the
single entries in Python by ptr[j]. Instead the interpreter returns
with the error message
WindowsError: exception: access violation reading 0x000000
WARNING: Failure executing file: <ctypes_matrix.py>
Furthermore, I am wondering if there is a fast way to use a numpy 2D
array instead or alternatively to cast the ctypes array into a numpy
array.
Has someone an idea to help me?
Thank you very much
Daniel