0

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?

Adel Khayata
  • 2,717
  • 10
  • 28
  • 46
  • 1
    Are the SDK's headers available anywhere? – AKX Jul 28 '20 at 11:57
  • I found this link that contains the header of their SDK, https://github.com/paulotovo/digitalpersona/blob/master/SDK%20Linux/sdk-porting-linux-rpi/opt/DigitalPersona/UareUSDK/Include/dpfpdd.h – Adel Khayata Jul 28 '20 at 12:04

1 Answers1

1

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);
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Great catch :), After changing my code to your suggestion, I am not getting the exception anymore, but I am getting back a result of 96075797 on return of the function which means "One or more parameters passed to the API call are invalid", is there a problem with the way I am passing the first parameter? – Adel Khayata Jul 28 '20 at 12:26
  • Also : should I change this line : ```dpfpdd_open.argtypes = [ctypes.c_char_p, ctypes.c_void_p]``` to be : ```dpfpdd_open.argtypes = [ctypes.c_char_p, ctypes.POINTER(ctypes.c_void_p)]``` – Adel Khayata Jul 28 '20 at 12:28
  • 1
    Yes, you the argtypes should be corrected too. I think the first parameter is fine, so long as its value is correct. You may need to use `dpfpdd_query_devices` to figure out what to pass it... – AKX Jul 28 '20 at 12:36