En Fri, 17 Oct 2008 20:03:44 -0300, Aaron "Castironpi" Brady
<[EMAIL PROTECTED]> escribió:
On Oct 16, 9:10 am, Hongtian <[EMAIL PROTECTED]> wrote:
Not exactly.
In my C/C++ application, I have following function or flow:
void func1(....)
{
call PyFunc(struct Tdemo, struct &Tdemo1);
}
I mean I want to invoke Python function 'PyFunc' and transfer a data
structure 'Tdemo' to this function. After some process in Python, I
want it return 'Tdemo1' back to the C/C++ application.
I research boost.python and think it is not a reasonable solution
because it make the C/C++ application too complex.
Thanks.
snip
Solution produced here. Includes dirty kludge, which will welcome
correction.
/C file:
#include <Python.h>
typedef struct {
int a;
float b;
} TypeA;
static PyObject *
methA(PyObject *self, PyObject *args) {
TypeA a;
TypeA b;
PyObject* fun;
PyObject* res;
PyArg_ParseTuple( args, "O", &fun );
a.a= 10;
a.b= 20.5;
res= PyObject_CallFunction( fun, "II", &a, &b );
printf( "%i %f\n", b.a, b.b );
Py_DECREF( res );
return PyInt_FromLong( 0 );
}
static PyMethodDef module_methods[] = {
{"methA", methA, METH_VARARGS, "No doc"},
{NULL, NULL, 0, NULL} /* Sentinel */
};
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
PyMODINIT_FUNC
initng27ext(void)
{
PyObject* m;
m = Py_InitModule3("ng27ext", module_methods,
"Custom.");
if (m == NULL)
return;
}
/Py file:
import ng27ext
import ctypes as c
class TypeA( c.Structure ):
_fields_= [
( 'a', c.c_int ),
( 'b', c.c_float )
]
from _ctypes import _cast_addr
_data_cast= c.PYFUNCTYPE( c.py_object, c.c_void_p, c.py_object,
c.py_object)( _cast_addr ) #dirty kludge
def exposed( obj1, obj2 ):
cob1= _data_cast( obj1, None, c.POINTER( TypeA ) )
cob2= _data_cast( obj2, None, c.POINTER( TypeA ) )
print cob1.contents.a, cob1.contents.b
cob2.contents.a= c.c_int( 60 )
cob2.contents.b= c.c_float( 70.5 )
print cob2.contents.a, cob2.contents.b
print ng27ext.methA( exposed )
/Compile & link:
c:/programs/mingw/bin/gcc ng27ext.c -c -Ic:/programs/python25/include
c:/programs/mingw/bin/gcc -shared ng27ext.o -o ng27ext.pyd -Lc:/
programs/python25/libs -lpython25
/Output:
10 20.5
60 70.5
60 70.500000
0
Press any key to continue . . .
--
http://mail.python.org/mailman/listinfo/python-list
--
Gabriel Genellina
--
http://mail.python.org/mailman/listinfo/python-list