我正在尝试转换 C 函数以供 python 3.6 使用。
代码如下:
lib = ctypes.WinDLL('ftrScanAPI.dll') # provided by fingerprint scanner
class FTRSCAN_IMAGE_SIZE(ctypes.Structure):
_fields_ = [
("nWidth", ctypes.c_int),
("nHeight", ctypes.c_int),
("nImageSize", ctypes.c_int)
]
print('Open device and get device handle...')
hDevice = lib.ftrScanOpenDevice()
print('handle is', hDevice)
print('Get image size...')
Image_size = FTRSCAN_IMAGE_SIZE(0, 0, 0)
if lib.ftrScanGetImageSize(hDevice, ctypes.byref(Image_size)):
print('Get image size succeed...')
print(' W', Image_size.nWidth)
print(' H', Image_size.nHeight)
print(' Size', Image_size.nImageSize)
else:
print('Get image size failed...')
函数定义:
typedef struct FTR_PACKED __FTRSCAN_IMAGE_SIZE {
int nWidth;
int nHeight;
int nImageSize;
} FTRSCAN_IMAGE_SIZE, *PFTRSCAN_IMAGE_SIZE;
FTRHANDLE ftrScanOpenDevice(); # typedef void * FTRHANDLE;
BOOL ftrScanGetImageSize(FTRHANDLE ftrHandle,
PFTR_SCAN_IMAGE_SIZE pImageSize);
但是具有相同代码的不同操作系统似乎有不同的结果:
我不打印“句柄在这里”
我尝试过的:
根据堆栈溢出的一些答案,这可能是由于未显式分配函数 argtypes 和 restype 造成的,所以我尝试并失败了。
最佳答案
在 99% 的情况下,参数之间的不一致(和/或返回)类型不一致是原因(查看 [SO]: C function called from Python via ctypes returns incorrect value (@CristiFati's answer) 了解更多详细信息)。
总是有[Python.Docs]: ctypes - A foreign function library for Python使用 CTypes 时打开。
我找到了 [GitHub]: erikssm/futronics-fingerprint-reader - (master) futronics-fingerprint-reader/ftrScanAPI.h (我不知道它与您目前拥有的有何不同,但到目前为止您发布的内容似乎是匹配的),我对您的代码做了一些更改:
为函数定义argtypes 和restype
定义缺失的类型(仅为了清楚起见)
其他一些微不足道的变化(重命名)
我在上述文件中注意到的另一件事是
#pragma pack(push, 1)
宏(查看 [MS.Docs]: pack 了解更多详情)。对于这个结构,它没有区别(感谢@AnttiHaapala 的提示),因为 3 int(4 字节)成员对齐不会改变,但对于其他结构(对于“较小”的成员类型(例如 char、short),您可能需要添加:_pack_ = 1
您修改后的代码(不用说,我没有运行它,因为我没有 .dll):
#!/usr/bin/env python
import ctypes as ct
from ctypes import wintypes as wt
# ...
class FTRSCAN_IMAGE_SIZE(ct.Structure):
# _pack_ = 1
_fields_ = (
("nWidth", ct.c_int),
("nHeight", ct.c_int),
("nImageSize", ct.c_int),
)
PFTRSCAN_IMAGE_SIZE = ct.POINTER(FTRSCAN_IMAGE_SIZE)
FTRHANDLE = ct.c_void_p
lib = ct.WinDLL("ftrScanAPI.dll") # provided by fingerprint scanner
ftrScanOpenDevice = lib.ftrScanOpenDevice
ftrScanOpenDevice.argtypes = ()
ftrScanOpenDevice.restype = FTRHANDLE
ftrScanGetImageSize = lib.ftrScanGetImageSize
ftrScanGetImageSize.argtypes = (FTRHANDLE, PFTRSCAN_IMAGE_SIZE)
ftrScanGetImageSize.restype = wt.BOOL
print("Open device and get device handle...")
h_device = ftrScanOpenDevice()
print("Handle is", h_device)
print("Get image size...")
image_size = FTRSCAN_IMAGE_SIZE(0, 0, 0)
if ftrScanGetImageSize(h_device, ct.byref(image_size)):
print("Get image size succeed...")
print(" W", image_size.nWidth)
print(" H", image_size.nHeight)
print(" Size", image_size.nImageSize)
else:
print("Get image size failed...")
关于不同操作系统上的python ctypes问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57789728/