How to get the output struct parameter by extending C function?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jul 28 06:34:42 CEST 2009


En Tue, 28 Jul 2009 00:06:06 -0300, jammy <huzaigang at cn.panasonic.com>  
escribió:

> I an just starting to use the ctypes module.
> Now I want to expentd C in python.
> In the function of C, I want to get the  output struct parameter which  
> is chaged in the function.

You're mixing a C extension with a ctypes call - why is that?
ctypes is mainly used when you *don't* want to actually write a C  
extension - if you actually write one, you don't need ctypes at all.

> ----------- 1.Header file of C(information.h)-----------
> typedef struct {
>  char  name[20];
>  int   height;
>  int   weight;
> } Infor;
> int getInfor( char* name, int height, int weight, Infor* infor);

> ----------- 5.test file of Python(test.py)-----------
>     Extest.getInfor(name, height, weight, byref(infor))

All arguments for a function intended to be called from Python must be  
PyObject*; byref is used to call a ctypes-wrapped function, not a function  
 from an extension module. (You're lucky you didn't crash the interpreter  
process)

Either use ctypes to directly call the getInfor function in information.c  
(and drop the extension module), or write a complete wrapper in the  
extension module and forget ctypes. Don't mix both ways.

-- 
Gabriel Genellina




More information about the Python-list mailing list