读书人

ptyhong调用DLL怎么使用结构体数组指

发布时间: 2013-01-05 15:20:40 作者: rapoo

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))


读书人网 >perl python

热点推荐