使用 Python 访问 Digital Persona DLL

Accessing Digital Persona DLL using Python

我正在尝试使用 Python 中的 ctypes 库访问 Digital Persona DLL 函数。

我写了这个测试代码:

import ctypes

dpfpddDll = ctypes.CDLL ("dpfpdd.dll")

# Library initialization.
dpfpdd_init = dpfpddDll.dpfpdd_init
dpfpdd_init.restype = ctypes.c_int
result = dpfpdd_init()

# Opening the device
device_name = "EB31330E-43BD-424F-B7FB-D454CCF90155"

dpfpdd_open = dpfpddDll.dpfpdd_open

dpfpdd_open.restype = ctypes.c_int

dpfpdd_open.argtypes = [ctypes.c_char_p, ctypes.c_void_p]

p1 = ctypes.c_char_p(device_name.encode("ascii")) # or device_name.encode("utf-8")

p2 = ctypes.c_void_p()

result = dpfpdd_open(p1,p2)

似乎库初始化工作正常,因为我得到的结果等于 0,这意味着成功,但是对于第二个函数调用,我得到 python 异常:

Exception has occurred: OSError
exception: access violation writing 0x0000000000000000

第二个函数的文档:

typedef void* DPFPDD_DEV

int DPAPICALL dpfpdd_open(  char *  dev_name,
                            DPFPDD_DEV *    pdev )
Opens a fingerprint reader in exclusive mode.

If you or another process have already opened the reader, you cannot open it again.

Parameters
dev_name    Name of the reader, as acquired from dpfpdd_query_devices().
pdev    [in] Pointer to empty handle (per DPFPDD_DEV); [out] Pointer to reader handle.
Returns
DPFPDD_SUCCESS: A valid reader handle is in the ppdev;
DPFPDD_E_FAILURE: Unexpected failure;
DPFPDD_E_INVALID_PARAMETER: No reader with this name found;
DPFPDD_E_DEVICE_BUSY: Reader is already opened by the same or another process;
DPFPDD_E_DEVICE_FAILURE: Failed to open the reader.

你能检查一下我的代码有什么问题吗?

您需要阅读声明

typedef void* DPFPDD_DEV
int DPAPICALL dpfpdd_open(char *dev_name, DPFPDD_DEV *pdev)

更近一点:-)

第二个参数实际上是 void** 类型(因为 DPFPDD_DEV 扩展为 void*)。

尝试

# ...
p2 = ctypes.c_void_p()

result = dpfpdd_open(p1, ctypes.pointer(p2))

– SDK 将在内部为设备结构分配内存并将其地址分配给您的 p2 指针。

这相当于C代码

DPFPDD_DEV dev;
dpfpdd_open(..., &dev);