Re: How to correctly pass “pointer-to-pointer” into DLL via ctypes?

zlchen.ken at gmail.com zlchen.ken at gmail.com
Sat Oct 27 10:22:56 EDT 2012


On Thursday, November 18, 2010 8:03:57 PM UTC+8, Grigory Petrov wrote:
> Hello.
> 
> I have a DLL that allocates memory and returns it. Function in DLL is like this:
> 
> void Foo( unsigned char** ppMem, int* pSize )
> {
>   * pSize = 4;
>   * ppMem = malloc( * pSize );
>   for( int i = 0; i < * pSize; i ++ ) (* pMem)[ i ] = i;
> }
> 
> Also, i have a python code that access this function from my 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 ]
> 
> I'm expecting that print will show "4 0 1 2 3" but it shows "4 221 221
> 221 221" O_O. Any hints what i'm doing wrong?

I am wondering in Python how you free the memory which is allocated in your DLL ?
Thanks 



More information about the Python-list mailing list