A little comments of ctypes and construct.

Aaron Brady castironpi at gmail.com
Fri Nov 21 05:30:17 EST 2008


On Nov 21, 2:28 am, 一首诗 <newpt... at gmail.com> wrote:
> Hi all,
>
> Recently I asked a question on this group:
>
> >> What's your choice when handle complicated C structures.
snip
>
> typedef struct _Point
> {
>    int x;
>    int y;
>
> } Point;
>
> typedef struct _Shape
> {
>   int z;
>   Point ap[2];
>
> }Shape;
snip
> 2. BigEndianStructure can not be nested.  So.  If you need bid endian,
> you got bad luck.

A bug is filed about this.

I have this code working:

import ctypes as c
class Point( c.LittleEndianStructure ):
    _fields_= [
        ( 'x', c.c_int ),
        ( 'y', c.c_int )
    ]

class Shape( c.LittleEndianStructure ):
    _fields_= [
        ( 'z', c.c_int ),
        ( 'ap', Point* 2 )
    ]

lib= c.WinDLL( 'ng36ext.pyd' )
lib.make.argtypes= [ ]
lib.make.restype= Shape
shape= lib.make( )
print shape.z, shape.ap[0].x, shape.ap[0].y, shape.ap[1].x, shape.ap
[1].y

/Output:

10 20 30 40 50
20
10 20 30 40 50

/Definition for 'make':

Shape make( void ) {
    Shape shape;
    shape.z= 10;
    shape.ap[ 0 ].x= 20;
    shape.ap[ 0 ].y= 30;
    shape.ap[ 1 ].x= 40;
    shape.ap[ 1 ].y= 50;
    printf( "%i %i %i %i %i\n", shape.z, shape.ap[0].x, shape.ap[0].y,
shape.ap[1].x, shape.ap[1].y );
    printf( "%i\n", sizeof( shape ) );
    return shape;
}

What is your next step?



More information about the Python-list mailing list