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

2012-10-27 Thread zlchen . ken
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 
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2010-11-19 Thread Diez B. Roggisch
Grigory Petrov grigory@gmail.com writes:

 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?

After correcting quite a few obvious errors in your code, it worked just
fine for me. So I guess what you think you test is not what you test.

--- test.py
from ctypes import *

foo = 
cdll.LoadLibrary(/Users/deets/projects/GH28/kinect/kinect/c/foo/libtest.dylib).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 ]
---

--- test.c
void foo( unsigned char** ppMem, int* pSize )
{
  int i;
 * pSize = 4;
 * ppMem = malloc( * pSize );
 for( i = 0; i  * pSize; i ++ ) (* ppMem)[ i ] = i;
}
---

--- CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (Test)
add_library(test SHARED test.c)
---

--
Diez
-- 
http://mail.python.org/mailman/listinfo/python-list