pythonctypesdigital-persona-sdk

Accessing Digital Persona DLL using Python


I am trying to access Digital Persona DLL functions using ctypes library in Python.

I have written this test code:

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)

It seems that library initialization is working fine because I am getting result equal to 0 which means success, but for the second function call I am getting python exception:

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

The documentation for the second function:

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.

Would you please check what is the problem with my code?


Solution

  • You'll need to read the declaration

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

    a little more closely :-)

    The second argument is actually of type void** (since DPFPDD_DEV expands to void*).

    Try

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

    ā€“ the SDK would then internally allocate memory for the device structure and assign its address to your p2 pointer.

    This is equivalent to the C code

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