【问题标题】:PYthon ctypes 'TypeError' LP_LP_c_long instance instead of _ctypes.PyCPointerTypePYthon ctypes 'TypeError' LP_LP_c_long 实例而不是 _ctypes.PyCPointerType
【发布时间】:2021-05-22 21:42:37
【问题描述】:

我尝试使用用 C++ 编写的 dll。它有这个功能:

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

我试过了:

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)

但它说:

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

我也尝试过使用双指针,但没有奏效。我可以用 ctypes 解决它还是不可能的?

【问题讨论】:

  • DEVICELAN_DEVICE的定义是什么?您还需要传递类型的 instances 而不是 types 并且应该声明 .argtypes.restypectypes也只能理解C接口,所以GetDeviceList函数必须是extern "C"

标签: python dll arguments ctypes


【解决方案1】:

错误消息是由于传递了 types 而不是 instances。您应该声明参数类型和返回类型,以便 ctypes 可以仔细检查传递的值是否正确。

这需要更多信息才能准确,但您需要的最低限度是:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2021-04-08
    • 2018-12-09
    相关资源
    最近更新 更多