1

我正在使用 Python 并尝试使用 WinUSB(来自 Microsoft)与这个简单的 USB 设备通信。我遇到了一个问题,因为为了找到设备,调用 setupAPI 函数 SetupDiEnumDeviceInterfaces 需要一个结构对象,我将其定义为一个类:

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

我尝试使用该属性,因为它给了我错误:

<type 'exceptions.TypeError'>: Don't know how to convert parameter

否则,我会读到像这样定义参数可以解决问题,但事实并非如此,而且我不确定在这里做什么。

我想使用 WinUSB,因为我只需要从设备中读取并写入设备,仅此而已,而且 WinUSB 似乎有我需要的东西,但是在我能解决这个问题之前,我有点卡住了

有什么建议么?如何将类对象传递给使用 ctypes.windll.LoadLibrary(DLL) 加载的 DLL 函数?

如果有更简单的方法可以做到这一点,我也完全赞成。

谢谢。

4

2 回答 2

2

正如@Roland 所说,您必须从ctypes.Structure. 这是一个工作版本:

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

输出:

_SP_DEV_INFO_DATA(cbsize=28,ClassGuid=08751880-13bb-11e2-96f0-402cf4ca5e51,DevInst=0x12345678L)
于 2012-10-11T15:57:59.167 回答
0

你的类应该派生自ctypes.Structure.

import ctypes

class _SP_DEVINFO_DATA(ctypes.Structure):

    _fields_ = [("cbSize", ctypes.c_ulong),
                ("ClassGuid", ctypes.c_ubyte * 12),
                ("DevInst", ctypes.c_ulong),
                ("Reserved", ctypes.c_voidp)]

    def __init__(self, guid, inst):
        self.cbSize = 24
        # GUID doesn't work like this... Needs proper conversion
        #self.ClassGuid = (ctypes.c_ubyte * 12)(bytearray(guid))
        self.DevInst = (ctypes.c_ulong)(inst)
        self.Reserved = None
于 2012-10-10T22:57:31.033 回答