[Numpy-discussion] numpy.ctypeslib.ndpointer and the restype attribute

Jens Rantil jens.rantil at telia.com
Mon Mar 23 09:36:22 EDT 2009


Hi all,

So I have a C-function in a DLL loaded through ctypes. This particular 
function returns a pointer to a double. In fact I know that this 
pointer points to the first element in an array of, say for simplicity, 
200 elements.

How do I convert this pointer to a NumPy array that uses this data (ie. 
no copy of data in memory)? I am able to create a numpy array using a 
copy of the data.

I have tried using the 'numpy.ctypeslib.ndpointer' but so far failed. 
In its documentation it claims it should be possible to use for not 
only argtypes attribute, but also restype. I have not found a single 
example of this on the web, and I wonder how this is done. As I see it, 
it would have to use the errcheck attribute to return an ndarray and 
not just restype.

My latest trial was:

>>> import ctypes
>>> pointer = DLL.my_func()
>>> ctypes_arr_type = C.POINTER(200 * ctypes.c_double)

>>> ctypes_arr = ctypes_arr_type(pointer)

>>> narray = N.ctypeslib.as_array(ctypes_arr)

however this didn't work.

Any hints would be appreciated.

Thanks,

Jens



More information about the NumPy-Discussion mailing list