【发布时间】:2019-11-14 23:05:06
【问题描述】:
我在 Python 代码中成功调用了一个 dll 库。所有的 By-Value 函数运行顺利。问题是我的 c 函数需要一个双精度数组的指针来返回结果。我不知道如何定义这个数组。
from ctypes import *
testlib = cdll.LoadLibrary(".\\testdll.dll")
def wrap_func(lib, funcname, restype, argtypes):
func = lib.__getattr__(funcname)
func.restype = restype
func.argtypes = argtypes
return func
test1 = wrap_func(testlib, 'testfun1', c_double, [c_double, POINTER(c_double), POINTER(c_char)])
test2 = wrap_func(testlib, 'testfun2', c_double, [c_double])
a = 2.5
b = Pointer(c_double)
tstr = Pointer(c_char)
d = test1(a, b, tstr)
print(b.values)
test1 有问题。 test2 成功运行。 原函数 test1 n C 为:
double testfun1(double x, double* y, char* str)
我希望函数的输出通过数组 b 恢复。 错误是:
ctypes.ArgumentError: argument 2: <class 'TypeError'>: expected LP_c_double instance instead of _ctypes.PyCPointerType
谁能帮帮我?
【问题讨论】:
-
如果
testfun1在y中返回一个双精度数组,它怎么知道这个数组有多长? -
仅供参考,
__getattr__并不意味着直接调用。请改用func = getattr(lib,funcname)。
标签: python c pointers dll ctypes