[python-win32] Passing c structure into Python script

Tim Roberts timr at probo.com
Thu Nov 19 18:40:51 CET 2009


a h wrote:
>  
> I want to know that how do I pass an C structure into python script.
> Actually my problem is
> 1. how do I call python script from C application ,in which I can pass
> C structure
> 2. how do I take that C structure into an python script
>  
> actually i have to write an python script which take an C structure
> and using pyasn, it encode that structure and dump the encoded data.
> also please provide some code snippets.

The Python documentation actually covers this fairly well, and there are
good samples on the web.  It's not as complicated as you might think. 
Start here:
   http://www.python.org/doc/
and check the "Extending and Embedding" links.

Basically, you end up creating an instance of the interpreter inside
your application by calling Py_Initialize, then you pass it Python
statements almost as if you were typing them at a command-line prompt. 
You'll have to do a certain amount of tedious work to convert the data
in C structure into a Python object or list, but there are examples. 
Also, you can just pass your structure as a raw set of bytes, and use
the "struct" module to parse the individual fields.

If you are using C++, the Boost libraries contain a Python wrapper that
make this almost painless.

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



More information about the python-win32 mailing list