PYthon ctypes ‘TypeError’ LP_LP_c_long instance instead of _ctypes.PyCPointerType

Total
1
Shares

I try to use a dll which is written in C++. It has this function:

bool PMDllWrapperClass::GetDeviceList(DEVICE** pDeviceArray, int* nDeviceCount, LAN_DEVICE** pLanDeviceArray, int LanDeviceCount, int InterfaceTypeToSearch)

I tried:

cP = ctypes.POINTER(ctypes.POINTER(ctypes.c_int64))
cIP = ctypes.POINTER(ctypes.c_int32)
cLP = ctypes.POINTER(ctypes.c_int32)
cDC = ctypes.c_int32()
cIS = ctypes.c_int32()


resultgetdev = PMDll.GetDeviceList(cP, cIP, cLP, cDC, cIS)

But it says:

ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected LP_LP_c_long instance instead of _ctypes.PyCPointerType

I also tried using a double Pointer, but non worked for it. Can I solve it with ctypes or is that not possible?


Solution

The error message is due to passing types instead of instances. You should declare the argument types and return type so ctypes can double-check the values passed are correct.

This needs more information to be accurate, but the minimum you need is:

test.cpp

#ifdef _WIN32
#   define API __declspec(dllexport)
#else
#   define API
#endif

struct DEVICE;
struct LAN_DEVICE;

extern "C" __declspec(dllexport)
bool GetDeviceList(DEVICE** pDeviceArray, int* nDeviceCount, LAN_DEVICE** pLanDeviceArray, int LanDeviceCount, int InterfaceTypeToSearch) {
    return true;
}

test.py:

from ctypes import *

class DEVICE(Structure):
    _fields_ = () # members??

class LAN_DEVICE(Structure):
    _fields_ = () # members??

dll = CDLL('./test')
dll.GetDeviceList.argtypes = POINTER(POINTER(DEVICE)), POINTER(c_int), POINTER(POINTER(LAN_DEVICE)), c_int, c_int
dll.GetDeviceList.restype = c_bool

device_list = POINTER(DEVICE)()     # create instances to pass by reference for output(?) parameters
landev_list = POINTER(LAN_DEVICE)()
dev_count = c_int()

lan_count = 5    # ??
search_type = 1  # ??

result = dll.GetDeviceList(byref(device_list),byref(dev_count),byref(landev_list),lan_count,search_type)
Leave a Reply

Your email address will not be published. Required fields are marked *