[python-win32] passing c structure into python function

Tim Roberts timr at probo.com
Wed Jan 13 19:35:20 CET 2010


a h wrote:
>
> thanks for your response
> in the last piece of c code i want to know how do i pass structure
> into python function in c. I have tried to call a python function
> which takes string as parameter.
>  
> i have passed string parameter as :
> const char *str_pArgs = "hello";
> pArgs = PyTuple_New(1);
> pValue = PyString_FromString(str_pArgs);
> PyTuple_SetItem(pArgs, 0,pValue);
> pValue = PyObject_CallObject(pFunc, pArgs);
>  
> similarly how do i convert this structure for python function argument.
>  
> ->pArgs = e //******************logic to assign struct employee into pArgs

There are several ways to do this.  If it were me, looking for the easy
way, I would just pass the structure as a string (by casting the address
of the struct to a (char *)), and unpack it with struct.unpack, as I
showed earlier.

There are many other ways, however.  You could create a tuple from the
struct elements, and pass the elements in their native types.  You could
even go to the trouble of creating a dict or an object in your C code,
populate it, and return it.  It depends on how often this will be used
and who is your audience.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list