簡體   English   中英

如何使用ctypes在python中正確包裝C API?

[英]how to properly wrap C API in python using ctypes?

我已經使用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。

您已經在非工作代碼中定義了實例,而不是類型 代替:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM