[python-win32] passing c structure into python function

a h ah.kode at gmail.com
Mon Feb 15 10:25:59 CET 2010


Hi
thnks. i have used struct.unpack function for small c structure as the way
Tim has suggested.
i have an complex C structure, and passing this structure as a string and
then using struct.unpack() is very tough job.and also i have pointers in my
struct now.

i want that i have structure say like
struct emp
{
    int id;
    ...
{
and i just pass this into python function by converting c structure into
python PyObject and using PyObject_CallObject() i can pass this pyobject.

In python script i want that i can simply print these values like
def func(s):
    print s.id

Is it possible to do this way?. and i dont know how to convert C structure
into python object(PyObject). Please help me how do i achieve this

thanks
ah


On Thu, Jan 14, 2010 at 12:05 AM, Tim Roberts <timr at probo.com> wrote:

> 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.
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100215/53a56404/attachment.htm>


More information about the python-win32 mailing list