Hello,

I am unable to get the simple numpy.ctypeslib.ndpointer docstring example from <http://docs.scipy.org/doc/numpy/reference/routines.ctypeslib.html> working on Windows.

Given a DLL `foo.dll` that exports a function `int bar(double *)`, calling foo.bar using the np.ctypeslib.ndpointer approach always fails with a ctypes.ArgumentError:

In [5]: foo = ctypes.CDLL('foo.dll')
In [6]: foo.bar.argtpes = [np.ctypeslib.ndpointer(dtype=np.float64, ndim=1, flags='C_CONTIGUOUS')]
In [7]: a = np.array([1, 2, 3], dtype=np.float64)
In [8]: foo.bar(a)
ArgumentError: argument 1: <type 'exceptions.TypeError'>: Don't know how to convert parameter 1

This works as expected:

In [9]: c_double_p = ctypes.POINTER(ctypes.c_double)
In [10]: foo.bar.argtpes = [c_double_p]
In [11]: foo.bar(a.ctypes.data_as(c_double_p))
Out[11]: 0

What am I missing? I also tried ndpointer(), several numpy and Python versions, real DLLs, and argument types. A self contained script is attached.

Thank you,

Christoph
# use on Windows only

import ctypes
from distutils import ccompiler
import numpy as np

open('foo.c', 'w').write(
    "extern __declspec(dllexport) int __cdecl bar(double *p) { return 0; }")

compiler = ccompiler.new_compiler()
compiler.link_shared_lib(compiler.compile(['foo.c']), 'foo')

foo = ctypes.CDLL('foo.dll')  # np.ctypeslib.load_library('foo.dll', '.')

a = np.array([1, 2, 3], dtype=np.float64)

# this works
c_double_p = ctypes.POINTER(ctypes.c_double)
foo.bar.argtpes = [c_double_p]
foo.bar(a.ctypes.data_as(c_double_p))

# this fails
foo.bar.argtpes = [np.ctypeslib.ndpointer(dtype=np.float64, ndim=1, 
                                          flags='C_CONTIGUOUS')]
foo.bar(a)
_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to