How to transfer data structure or class from Python to C/C++?

Aaron Brady castironpi at gmail.com
Sun Dec 21 06:02:21 EST 2008


On Oct 16, 9:10 am, Hongtian <hongtian.i... at gmail.com> wrote:
snip
> 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.
snip

This is a correction of:
Oct 17, 5:03 pm, "Aaron \"Castironpi\" Brady" <castiro... at gmail.com>
http://groups.google.com/group/comp.lang.python/msg/f11ac4a34faaf766
Thread:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/68d59cb670a345ef

Revised solution uses 'from_address' instead of '_ctypes._cast_addr'.

import ng27ext

import ctypes as c
class TypeA( c.Structure ):
    _fields_= [
        ( 'a', c.c_int ),
        ( 'b', c.c_float )
        ]

def exposed( pobj1, pobj2 ):
    obj1= TypeA.from_address( pobj1 )
    obj2= TypeA.from_address( pobj2 )
    print obj1.a, obj1.b
    obj2.a= c.c_int( 60 )
    obj2.b= c.c_float( 65.5 )
    print obj2.a, obj2.b

print ng27ext.methA( exposed )



More information about the Python-list mailing list