ptyhong调用DLL,怎么使用结构体数组指针做参数
1个回答
展开全部
ptyhong调用DLL,如何使用结构体数组指针做参数
C++函数原型
typedef struct
{
unsigned long DeviceType;
int Handle;
int NumberOfClients;
int SerialNumber;
int MaxAllowedClients;
}NeoDevice;
int _stdcall icsneoFindNeoDevices(unsigned long DeviceTypes, NeoDevice *pNeoDevices, int *pNumberOfDevices);
使用python如下:
class NeoDevice(Structure):
_fields_ = [("DeviceType",c_ulong),
("Handle",c_int),
("NumberOfClients",c_int),
("SerialNumber",c_int),
("MaxAllowedClients",c_int)]
class cNeoVICan(CCanBase):
def __init__(self):
neoVi = windll.icsneo40
self.icsneoFindNeoDevices = neoVi.icsneoFindNeoDevices
if __name__ == "__main__":
canBus = cNeoVICan()
print canBus.icsneoGetDLLVersion()
iNumberOfDevices = [NeoDevice() for x in range(10)]
num = c_int
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), pointer(iNumberOfDevices), byref(num))
但是会报如下错误:
Traceback (most recent call last):
File "C:\Work\Project\GUI\wxPyCANC303\Drv\source\src\drv\neoVI\cNeoVICan.py", line 224, in <module>
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), pointer(iNumberOfDevices), byref(num))
TypeError: _type_ must have storage info
请问是什么错误原因啊?
谢谢。
------解决方案--------------------
因为python的list不是一个ctypes类型
正确做法是
class NeoDevice(Structure):
_fields_ = [("DeviceType",c_ulong),
("Handle",c_int),
("NumberOfClients",c_int),
("SerialNumber",c_int),
("MaxAllowedClients",c_int)]
class cNeoVICan(CCanBase):
def __init__(self):
neoVi = windll.icsneo40
self.icsneoFindNeoDevices = neoVi.icsneoFindNeoDevices
if __name__ == "__main__":
canBus = cNeoVICan()
print canBus.icsneoGetDLLVersion()
iNumberOfDevices = (NeoDevice * 10)()
num = c_int()
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), cast(iNumberOfDevices, POINT(NeoDevice)), byref(num))
C++函数原型
typedef struct
{
unsigned long DeviceType;
int Handle;
int NumberOfClients;
int SerialNumber;
int MaxAllowedClients;
}NeoDevice;
int _stdcall icsneoFindNeoDevices(unsigned long DeviceTypes, NeoDevice *pNeoDevices, int *pNumberOfDevices);
使用python如下:
class NeoDevice(Structure):
_fields_ = [("DeviceType",c_ulong),
("Handle",c_int),
("NumberOfClients",c_int),
("SerialNumber",c_int),
("MaxAllowedClients",c_int)]
class cNeoVICan(CCanBase):
def __init__(self):
neoVi = windll.icsneo40
self.icsneoFindNeoDevices = neoVi.icsneoFindNeoDevices
if __name__ == "__main__":
canBus = cNeoVICan()
print canBus.icsneoGetDLLVersion()
iNumberOfDevices = [NeoDevice() for x in range(10)]
num = c_int
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), pointer(iNumberOfDevices), byref(num))
但是会报如下错误:
Traceback (most recent call last):
File "C:\Work\Project\GUI\wxPyCANC303\Drv\source\src\drv\neoVI\cNeoVICan.py", line 224, in <module>
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), pointer(iNumberOfDevices), byref(num))
TypeError: _type_ must have storage info
请问是什么错误原因啊?
谢谢。
------解决方案--------------------
因为python的list不是一个ctypes类型
正确做法是
class NeoDevice(Structure):
_fields_ = [("DeviceType",c_ulong),
("Handle",c_int),
("NumberOfClients",c_int),
("SerialNumber",c_int),
("MaxAllowedClients",c_int)]
class cNeoVICan(CCanBase):
def __init__(self):
neoVi = windll.icsneo40
self.icsneoFindNeoDevices = neoVi.icsneoFindNeoDevices
if __name__ == "__main__":
canBus = cNeoVICan()
print canBus.icsneoGetDLLVersion()
iNumberOfDevices = (NeoDevice * 10)()
num = c_int()
iResult = canBus.icsneoFindNeoDevices(c_ulong(65535), cast(iNumberOfDevices, POINT(NeoDevice)), byref(num))
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询