Python und ctypes: wie man richtig pass "pointer-to-pointer" in der DLL?

Ich habe eine DLL, die Speicher zuweist und gibt es zurück. Funktion in der DLL ist wie folgt:

void Foo( unsigned char** ppMem, int* pSize )
{
  * pSize = 4;
  * ppMem = malloc( * pSize );
  for( int i = 0; i < * pSize; i ++ ) (* ppMem)[ i ] = i;
}

Außerdem habe ich ein python-code, der Zugriff auf diese Funktion aus meiner DLL:

from ctypes import *
Foo = windll.mydll.Foo
Foo.argtypes = [ POINTER( POINTER( c_ubyte ) ), POINTER( c_int ) ]
mem = POINTER( c_ubyte )()
size = c_int( 0 )
Foo( byref( mem ), byref( size ) ]
print size, mem[ 0 ], mem[ 1 ], mem[ 2 ], mem[ 3 ]

Ich gehe davon aus, dass print zeigen "4 0 1 2 3" aber es zeigt, "4 221 221 221 221" O_O. Irgendwelche Hinweise, was ich falsch mache?

InformationsquelleAutor der Frage grigoryvp | 2010-11-18

Schreibe einen Kommentar