Skip to content Skip to sidebar Skip to footer

How To Use Ctypes Void ** Pointer In Python3

I would to connect a spectrometer by its DLL, one of function is defined as UINT UAI_SpectrometerOpen(unsigned int dev, void** handle, unsigned int VID, unsigned int PID) from do

Solution 1:

According to your error output:

  File "C:/Users/Steve/Documents/Python Scripts/otoDLL.py", line 5, in <module>
    spectrometerOpen.argtypes = (ctypes.c_uint, ctypes.POINTER(c_void_p),

You forget to put ctypes before c_void_p, thus:

spectrometerOpen.argtypes = (ctypes.c_uint, ctypes.POINTER(ctypes.c_void_p),
                         ctypes.c_uint, ctypes.c_uint)

According to your function signature , the handle parameter is a pointer to a void*, thus you need to pass it like this:

import ctypes
otoDLL = ctypes.CDLL('UserApplication.dll')
spectrometerOpen = otoDLL.UAI_SpectrometerOpen
spectrometerOpen.argtypes = (ctypes.c_uint, ctypes.POINTER(ctypes.c_void_p),
                         ctypes.c_uint, ctypes.c_uint)
spectrometerOpen.restypes = ctypes.c_uint

# declare HANDLE type, which is a void*
HANDLE = ctypes.c_void_p

# example: declare an instance of HANDLE, set to NULL (0)
my_handle = HANDLE(0)

#pass the handle by reference (works like passing a void**)
errorCode = spectrometerOpen(0, ctypes.byref(my_handle), 1592, 2732)

Note: this is just an example, you should check the documentation of the spectrometerOpen function to see what exactly it is awaiting exactly for the handle parameter (can it be NULL, what type is it exactly, etc.).

Post a Comment for "How To Use Ctypes Void ** Pointer In Python3"