我是一名使用 Python 中的 ctypes 的新手编码人员,并尝试使用用 C 编写的 DLL 中的函数。我在 SO 上发现了很多与我类似的问题,但没有任何答案可以回答此类难题。
我已经很好地加载了 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 函数中获取所需双精度的正确方向,我将永远感激不已;我很困惑,无法将其付诸实践。