相当于"char name [MAX_DEVICE_NAME_LENGTH]"的ctypes



在我正在进行的从python访问Digital Persona DLL函数的测试中,我被以下代码卡住了:

import ctypes
# Load DLL into memory.
# dpfpddDll = ctypes.WinDLL ("dpfpdd.dll")
dpfpddDll = ctypes.CDLL ("dpfpdd.dll")

# Library initialization.
dpfpdd_init = dpfpddDll.dpfpdd_init
dpfpdd_init.restype = ctypes.c_int
result = dpfpdd_init()
print(f"Initialize result: {result}")
class dpfpdd_hw_descr_struct(ctypes.Structure):
_fields_ = [('vendor_name', ctypes.c_char_p),
('product_name', ctypes.c_char_p),
('serial_num', ctypes.c_char_p)] 
class dpfpdd_hw_id_struct(ctypes.Structure):
_fields_ = [('vendor_id', ctypes.c_ushort),
('product_id', ctypes.c_ushort)] 
class dpfpdd_ver_info_struct(ctypes.Structure):
_fields_ = [('major', ctypes.c_int),
('minor', ctypes.c_int),
('maintenance', ctypes.c_int)] 
class dpfpdd_hw_version_struct(ctypes.Structure):
_fields_ = [('hw_ver', dpfpdd_ver_info_struct),
('fw_ver', dpfpdd_ver_info_struct),
('bcd_rev', ctypes.c_ushort)]     
class dpfpdd_dev_info_struct(ctypes.Structure):
_fields_ = [('size', ctypes.c_uint),
('name', ctypes.c_char_p),
('descr', dpfpdd_hw_descr_struct),
('id', dpfpdd_hw_id_struct),
('ver', dpfpdd_hw_version_struct),
('modality', ctypes.c_uint),
('technology', ctypes.c_uint)] 
dpfpdd_query_devices = dpfpddDll.dpfpdd_query_devices
dpfpdd_query_devices.restype = ctypes.c_int
dpfpdd_query_devices.argtypes = [ctypes.POINTER(ctypes.c_uint), ctypes.POINTER(dpfpdd_dev_info_struct)]
p1 = ctypes.c_uint(10)
p2 = dpfpdd_dev_info_struct()
result = dpfpdd_query_devices(ctypes.byref(p1),ctypes.byref(p2))
print("dpfpdd_query_devices result: " + str(result))
print(f"p1: {p1}")
print(f"p2: {p2}")
print(f"p2.size: {p2.size}")
# print(f"p2.name: {p2.name}")
print(f"p2.descr: {p2.descr}")
print(f"p2.id.vendor_id: {p2.id.vendor_id}")
print(f"p2.ver.hw_ver.major: {p2.ver.hw_ver.major}")
print(f"p2.modality: {p2.modality}")
print(f"p2.technology: {p2.technology}")

# Library release.
dpfpdd_exit = dpfpddDll.dpfpdd_exit
dpfpdd_exit.restype = ctypes.c_int
result = dpfpdd_exit()
print(f"Un Initialize result: {result}")

这段代码打印以下内容:

Initialize result: 0
dpfpdd_query_devices result: 0
p1: c_ulong(1)
p2: <__main__.dpfpdd_dev_info_struct object at 0x00000227FB9BAA40>
p2.size: 1452
p2.descr: <__main__.dpfpdd_hw_descr_struct object at 0x00000227FB9BAAC0>
p2.id.vendor_id: 11590
p2.ver.hw_ver.major: 1143816774
p2.modality: 0
p2.technology: 0
Un Initialize result: 0

调用似乎是成功的,因为我能够从返回的参数p2中读取一些字段,但一旦我取消注释这一行:

print(f"p2.name: {p2.name}")

我只得到这个打印出来:

Initialize result: 0
dpfpdd_query_devices result: 0
p1: c_ulong(1)
p2: <__main__.dpfpdd_dev_info_struct object at 0x00000228B0419A40>
p2.size: 1452

因此,访问p2.name似乎导致了python中的一个问题,并导致停止!

是我的领域宣言";name";在类dpfpdd_dev_info_struct中是否正确?

('name', ctypes.c_char_p)

此字段在SDK标头中定义为:

#define     MAX_DEVICE_NAME_LENGTH   1024
char    name [MAX_DEVICE_NAME_LENGTH]

我感谢你的帮助。

谢谢。

我通过如下声明名称变量来实现它:

('name', ctypes.c_char * 1024)

相关内容

  • 没有找到相关文章

最新更新