首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

ptyhong调用DLL,怎么使用结构体数组指针做参数

2013-01-05 
ptyhong调用DLL,如何使用结构体数组指针做参数C++函数原型typedef struct{unsigned long DeviceTypeint H

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


热点排行