我尝试使用用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我也试过使用双指针,但non也为此而工作。我能用ctype来解决这个问题吗?
发布于 2021-05-25 19:26:53
错误消息是由于传递类型而不是实例造成的。您应该声明参数类型和返回类型,这样ctype就可以重复检查传递的值是否正确。
这需要更多的信息才能准确,但您需要的最低要求是:
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)https://stackoverflow.com/questions/67654418
复制相似问题