[python-win32] passing c structure into python function

a h ah.kode at gmail.com
Wed Jan 13 15:31:43 CET 2010


Hi
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

regards
ah

On Tue, Jan 12, 2010 at 5:33 PM, a h <ah.kode at gmail.com> wrote:

> Hi
> I have witten C code which call a function defined in python, passing an
> structure, but couldn't know how to pass c structure into a python
> function(data conversion) and how to write python script function which
> takes an c structure and fill value into it (commented portion)
>
> I have looked at struct module but couldn't understand that how it works
> for below.
>
> typedef struct employee
> {
>  char emp_name[10];
>  int emp_ID;
> }emp;
>
> int main()
> {
> emp e = {0};
> Pyobject *pName, *pModule, *pFunc, *pArgs,*pValue;
> Py_Initialize();
> pName = PyString_FromString("test");
> pModule = PyImport_Import(pName);
> Py_DECREF(pName);
> if (pModule != NULL)
> {
> pFunc = PyObject_GetAttrString(pModule, "testStruct");
> if(pFunc && PyCallable_Check(pFunc))
> {
> pArgs = e //******************logic to assign struct employee into pArgs
> pValue = PyObject_CallObject(pFunc, pArgs);
> }
> }
> Py_DECREF(pValue);
> Py_DECREF(pModule);
> Py_Finalize();
> return 0;
> }
>
> and now python script test.py
> def testStruct():    # **** need any parameter will be specify
> #**** assign value into the cStructure
> f=open(filename)
> f.write(cStructure.Value) # **** value to write in file
>
> Thanks in advance
> regards
> ah
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100113/892fd93b/attachment.htm>


More information about the python-win32 mailing list