I'm working with Python and trying to talk to this simple USB device using WinUSB (from Microsoft). I'm having a problem since in order to find the device, calling the setupAPI function SetupDiEnumDeviceInterfaces takes a struct object, which I defined as a class:
class _SP_DEVINFO_DATA:
def __init__(self, ClassGUID, DevInst = ""):
'''flags = SPINT_DEFAULT, SPINT_REMOVED, or SPINT_ACTIVE'''
self._ClassGUID = None
self._DevInst = None
self._Reserved = None
self._cbSize = None
###Getters:
def getClassGUID(self):
return self._ClassGUID
def getDevInst(self):
return self._DevInst
def getReserved(self):
return self._Reserved
def getcbSize(self):
return self._cbSize
###Setters:
def setClassGUID(self, value):
self._ClassGUID = value
def setDevInst(self, value):
self._DevInst = value
def setReserved(self, value):
self._Reserved = value
def setcbSize(self):
self._cbSize = sys.getsizeinfo(self)
ClassGUID = property(getClassGUID, setClassGUID, None, "Class GUID")
DevInst = property(getDevInst, setDevInst, None, "Device Instance")
Reserved = property(getReserved, setReserved, None, "RESERVED: DO NOT USE")
cbSize = property(getcbSize, setcbSize, None, "CB Size. Set automatically")
I tried using the property because it gave me the error :
<type 'exceptions.TypeError'>: Don't know how to convert parameter
otherwise, and I'd read that defining parameters like this would resolve the problem, but it doesn't, and I'm not certain what to do here.
I want to use WinUSB because I only need to read from the device and write to the device, and that's it, and WinUSB seems to have what I need, but until I can get past this problem, I'm kind of stuck
Any suggestions? How do I pass a class object to a DLL function loaded with ctypes.windll.LoadLibrary(DLL)?
And if there's an easier way to do this, I'm all for that too.
Thanks.
As @Roland said, you must derive from ctypes.Structure
. Here's a working version:
import ctypes
from ctypes import wintypes
import uuid
class _SP_DEVINFO_DATA(ctypes.Structure):
_fields_ = [("cbSize", wintypes.DWORD),
("ClassGuid", ctypes.c_char * 16),
("DevInst", wintypes.DWORD),
("Reserved", wintypes.LPVOID)]
def __init__(self, guid, inst):
self.cbSize = ctypes.sizeof(_SP_DEVINFO_DATA)
self.ClassGuid = uuid.UUID(guid).get_bytes()
self.DevInst = (ctypes.c_ulong)(inst)
self.Reserved = None
def __repr__(self):
return "_SP_DEV_INFO_DATA(cbsize={},ClassGuid={},DevInst={})".format(
self.cbSize,uuid.UUID(bytes=self.ClassGuid),hex(self.DevInst))
sp = _SP_DEVINFO_DATA('08751880-13bb-11e2-96f0-402cf4ca5e51',0x12345678)
print sp
Output:
_SP_DEV_INFO_DATA(cbsize=28,ClassGuid=08751880-13bb-11e2-96f0-402cf4ca5e51,DevInst=0x12345678L)