Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用ctypes在python中正确包装C API?_Python_C_Ctypes_Word Wrap - Fatal编程技术网

如何使用ctypes在python中正确包装C API?

如何使用ctypes在python中正确包装C API?,python,c,ctypes,word-wrap,Python,C,Ctypes,Word Wrap,我已经使用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

我已经使用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)
我认为指针是正确的,因为当我告诉代码打印(g)时,它会返回SP对象的位置,如下所示:

<__main__.LP_SP object at 0x000001D1EAB862C8>

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

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

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

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

dll.readSP.argtypes = POINTER(File), c_char_p, c_double, c_double, c_double, POINTER(TTag), c_ushort

<代码>文件/代码>是如何定义的?在那些似乎是一个语句的中间的那些断线是什么?或者它们不是一个语句吗?再看一遍,它们作为一种陈述或其他任何东西都没有多大意义,真的。
dll.readSP.argtypes=POINTER(File)、c\u char\u p(PN)、
dll.readSP.restype=POINTER(SP)
之间的那些东西应该是什么?谢谢,我以为这就是问题所在,但我不确定。你的回答解决了我遇到的一个问题,但现在我调用dll时出错了。我得到一个
TypeError这个函数至少需要2个参数(1个给定)
这个错误发生在我调用dll时,调用看起来像这样
g=readSP(MF)
参数到底是什么意思@101and文件的定义如下(为了方便起见缩短):
类文件(结构):\u字段\=[('name',ctypes.c\u char\u p),('header',ctypes.POINTER(header)),('FInfo',ctypes.POINTER(FInfo)),
参数是函数调用括号内的值,例如,
MF
是您的第一个也是唯一一个参数。它需要另一个参数。您是否有指向所调用DLL文档的链接?