如何使用ctypes在python中正确地包装capi?

2024-09-27 04:23:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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

这一个有效:

^{pr2}$

这个不会:

#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)

我认为指针是正确的,因为当我告诉代码要打印(g)时,它会返回SP对象的位置,如下所示:

<__main__.LP_SP object at 0x000001D1EAB862C8>

但它不能返回SP文件结构中其他任何内容的位置。这让我相信问题在于如何包装API。但我不知道我哪里出了问题。在

我想我一定没有正确地包装API,但我不确定哪里出了问题。double*refTIME和struct TimeTage*refGMT在C代码中为空,因此我在脚本中将它们设置为0。在


Tags: topathimportapivaluedtctypessp
1条回答
网友
1楼 · 发布于 2024-09-27 04:23:46

您在非工作代码中定义了实例,而不是类型。而不是:

dll.readSP.argtypes = POINTER(File), c_char_p(PN), c_double(beginTime), c_double(endTime), 0, 0, c_ushort(convert),

您需要:

^{pr2}$

相关问题 更多 >

    热门问题