[python-win32] passing c structure into python function

a h ah.kode at gmail.com
Tue Jan 12 13:03:07 CET 2010


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/20100112/ae484ba7/attachment.htm>


More information about the python-win32 mailing list