[ctypes-users] Functions returning a Struct
Brought to you by:
theller
From: <wr...@hu...> - 2007-07-26 06:42:25
|
I was attempting to learn ctypes by wrapping several functions from libgls. Everything was working smoothly until I got several functions that returned structures. the c code looked like this: typedef struct { double dat[2]; } gsl_complex; gsl_complex gsl_complex_rect (double x, double y) { /* return z = x + i y */ gsl_complex z; GSL_SET_COMPLEX (&z, x, y); return z; } #GSL_SET_COMPLEX is a macro defined as #define GSL_SET_COMPLEX(zp,x,y) do {(zp)->dat[0]=(x); (zp)->dat[1]=(y);} while(0) I tried to access this function with the following python code: import ctypes gsl = ctypes.cdll.libgsl class gsl_complex(ctypes.Structure): _fields_ = [("dat", ctypes.c_double*2)] gsl_complex_rect = gsl.gsl_complex_rect gsl_complex_rect.argtypes = [ctypes.c_double, ctypes.c_double] gsl_complex_rect.restype = gsl_complex arg1 = ctypes.c_double(1.2) arg2 = ctypes.c_double(3.4) complex_num = gsl_complex_rect(arg1, arg2) this results in an exception: ValueError: Procedure called with not enough arguments (4 bytes missing) or wrong calling convention The arguments seem to be correct, and I successfully called other functions with this calling convention. changing to stdcall or windll did not fix the problem. Any suggestions? Thanks in advance, Wilton |