【发布时间】:2017-07-07 21:04:57
【问题描述】:
我使用 ctypes 在 Python 中封装了来自 C 库的两个 API。一个有效,另一个无效。我的问题是:如何使用 ctypes 从 Python 中的 C 库中正确包装 API?
一些背景信息:
import ctypes
from ctypes import *
MF = (b'path_to_file')
beginTime = (-Value)
endTime = (Value)
PN = (b'path_to_String')
DT_RETURNGMT = 0x0100
DT_FLOAT = 0x0001
convert = DT_RETURNGMT|DT_FLOAT
这个有效:
#read functions
dll.openFile.argtypes = c_char_p,
dll.openFile.restype = POINTER(File)
dll.freeFile.argtypes = POINTER(File),
dll.freeFile.restype = None
f = dll.openFile(MF)
print(f[0].fileInfo[0].items)
这个没有:
#retrieve data
dll.readSP.argtypes = POINTER(File), c_char_p(PN),
c_double(beginTime), c_double(endTime),
0, 0,
c_ushort(convert),
dll.readSP.restype = POINTER(SP)
#pointer to the SP file structure
g = dll.readSP(f, MF)
print(g)
print(g[0].points)
pint(g[0].tStamp[0].data[0].TTag)
我认为指针是正确的,因为当我告诉代码 print(g) 时,它会返回我的 SP 对象的位置,如下所示:
<__main__.LP_SP object at 0x000001D1EAB862C8>
但它不能返回 SP 文件结构中其他任何内容的位置。这让我相信问题在于我如何包装 API。但我不确定我哪里出错了。
我认为我一定没有正确包装 API,但我不确定我哪里出错了。 double *refTIME 和 struct TimeTage *refGMT 在 C 代码中为 NULL,因此我在脚本中将它们设为 0。
【问题讨论】:
-
File是如何定义的? -
在似乎打算作为一个语句的中间的所有这些换行符是怎么回事?
-
或者他们不打算成为一个声明?再看一遍,它们作为一个陈述或其他任何东西真的没有多大意义。
dll.readSP.argtypes = POINTER(File), c_char_p(PN),和dll.readSP.restype = POINTER(SP)之间的那些东西应该是什么?