Problem on ctypes arguments in a DLL function
jfong at ms4.hinet.net
jfong at ms4.hinet.net
Fri Dec 18 03:41:43 EST 2015
I am trying to use the libusb-win32 v1.2.6.0 with Win7. I wrote a test program(showing below) but stuck with a strange problem. Here is the result:
----------------------------
D:\Work\Python34>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import libusb_u2aTest as u2a
0x10c4
0x1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "D:\Work\Python34\libusb_u2aTest.py", line 364, in <module>
pHDL = _lib.usb_open(pDevice)
ValueError: Procedure probably called with too many arguments (4 bytes in excess
)
>>>
-----------------
I replace the troubled line with a "pass" to make the loading success, then to check the type of "pDevice", it's correct.
>>> u2a.pDevice
<libusb_u2aTest.LP__usb_device object at 0x017F0120>
It seems no reason that this call can be in trouble:-( Any hint was appreciated. Thanks ahead.
libusb_u2aTest.py listing:
-----------------------------
from ctypes import *
_lib = windll.LoadLibrary("C:\\Windows\\System32\\libusb0.dll")
_PATH_MAX = 512
### Data structures...
class _usb_device_descriptor(Structure):
_fields_ = [('bLength', c_uint8),
('bDescriptorType', c_uint8),
('bcdUSB', c_uint16),
('bDeviceClass', c_uint8),
('bDeviceSubClass', c_uint8),
('bDeviceProtocol', c_uint8),
('bMaxPacketSize0', c_uint8),
('idVendor', c_uint16),
('idProduct', c_uint16),
('bcdDevice', c_uint16),
('iManufacturer', c_uint8),
('iProduct', c_uint8),
('iSerialNumber', c_uint8),
('bNumConfigurations', c_uint8)]
class _usb_device(Structure): pass
class _usb_bus(Structure): pass
_usb_device._fields_ = [('next', POINTER(_usb_device)),
('prev', POINTER(_usb_device)),
('filename', c_int8 * _PATH_MAX),
('bus', POINTER(_usb_bus)),
('descriptor', _usb_device_descriptor),
#('config', POINTER(_usb_config_descriptor)), # not implemented yet
('config', POINTER(c_int)), # to ease this test
('dev', c_void_p),
('devnum', c_uint8),
('num_children', c_ubyte),
('children', POINTER(POINTER(_usb_device)))]
_usb_bus._fields_ = [('next', POINTER(_usb_bus)),
('prev', POINTER(_usb_bus)),
('dirname', c_char * _PATH_MAX),
('devices', POINTER(_usb_device)),
('location', c_uint32),
('root_dev', POINTER(_usb_device))]
_usb_dev_handle = c_void_p
### Function prototype...
# struct usb_bus *usb_get_busses(void);
_lib.usb_get_busses.restype = POINTER(_usb_bus)
# usb_dev_handle *usb_open(struct usb_device *dev);
_lib.usb_open.argtypes = [POINTER(_usb_device)]
_lib.usb_open.restype = _usb_dev_handle
### Test start...
_lib.usb_init()
_lib.usb_find_busses()
_lib.usb_find_devices()
pBus = _lib.usb_get_busses()
if bool(pBus):
pDevice = pBus[0].devices
if bool(pDevice):
print(hex(pDevice[0].descriptor.idVendor))
print(hex(pDevice[0].descriptor.idProduct))
if pDevice[0].descriptor.idVendor == 0x10c4 and \
pDevice[0].descriptor.idProduct == 0x0001:
pHDL = _lib.usb_open(pDevice) # <--this line is in trouble
More information about the Python-list
mailing list