【发布时间】:2018-06-23 14:58:27
【问题描述】:
我是一个在 Python 中使用 ctypes 的新手编码器,并尝试使用用 C 编写的 DLL 中的函数。我发现了很多类似的问题,但没有任何东西可以回答这种类型的难题。
我已经很好地加载了 DLL,但是我需要调用的函数之一要求一个指向 6 个双精度数组的指针以将值转储到其中,我无法弄清楚如何通过 Python 为函数提供它需要的东西. (我在 C 中尝试这样做的失败是另一回事。)
我尝试了 ctypes.POINTER(c_double) 的各种排列,使用 byref()、POINTER(c_double * 6) 等,最坏的情况是类型错误,或者最好的情况是访问冲突。
来自 DLL 文档:
int swe_calc_ut (double tjd_ut, int ipl, int iflag, double* xx, char* serr)
该函数使用儒略日中的时间进行计算,以双精度形式返回行星体的经度、纬度等。
我最接近传递 DLL 将接受的数据类型是使用此代码,只是尝试从 swe_calc_ut 中获取 6 个双精度值中的任何一个:
dll = windll.LoadLibrary(# file path)
# retype the args for swe_calc_ut
py_swe_calc_ut = dll.swe_calc_ut
py_swe_calc_ut.argtypes = [c_double, c_int, c_int, c_double, c_char_p]
py_swe_calc_ut.restype = None
tjd = c_double(# some value from elsewhere)
returnarray = c_double()
errorstring = create_string_buffer(126)
py_swe_calc_ut(tjd, c_int(0), c_int(64*1024), returnarray, errorstring)
当我尝试按原样运行时,出现错误:
OSError: exception: access violation writing 0x0000000000000000
使用 byref() 会给我一个类型错误,等等。
如果有人能指出正确的方向,让我从原始 DLL 函数中获得所需的双精度,我将永远感激不尽;我被难住了,无法把它搞定。
【问题讨论】: