python - ctypes cdecl gives 4 bytes missing when calling gcc compiled dll returning a struct -
i have c library (chipmunk) want call using ctypes. however, fails on functions return struct?
the error
file "qw.py", line 19, in <module> b = cpbbnew3(1,2,3,4) valueerror: procedure called not enough arguments (4 bytes missing) or wrong calling convention this (relevant) c-code: in cpbb.h
typedef struct cpbb { cpfloat l, b, r ,t; } cpbb; cpbb cpbbnew3(cpfloat l, cpfloat b, cpfloat r, cpfloat t); in cpbb.c
cpbb cpbbnew3(cpfloat l, cpfloat b, cpfloat r, cpfloat t) { cpbb bb = {l, b, r, t}; return bb; } compiled
gcc -o3 -std=gnu99 -shared -c
gcc -o3 -std=gnu99 -shared -s
then python looks
from ctypes import * chipmunk_lib = cdll.loadlibrary('''c:/code/pymunk/trunk/pymunk/chipmunk.dll''') class cpbb(structure): pass cpbb._pack_ = 4 cpbb._fields_ = [ ('l', c_double), ('b', c_double), ('r', c_double), ('t', c_double), ] cpbbnew3 = chipmunk_lib.cpbbnew3 cpbbnew3.restype = cpbb cpbbnew3.argtypes = [c_double, c_double, c_double, c_double] b = cpbbnew3(1,2,3,4) this particular example works if compile -mrtd , use windll (using stdcall). using stdcall on whole library creates segmentation faults in other parts of library when compiled optimizations , newish version of gcc why nice cdecl working.
depending on size of struct , compiler/platform, cdecl may return structs passing hidden pointer caller-allocated struct before normal parameters. try following:
b = cpbb() cpbbnew3(byref(b),1,2,3,4)
Comments
Post a Comment