【发布时间】:2026-02-24 03:15:01
【问题描述】:
我正在使用 Python 尝试创建一个简单的程序,该程序可以从一个相当简单的 USB 设备读取和写入数据。我遇到的问题是,由于 PyWinUSB 和 PyUSB 似乎没有我需要的东西(尝试写入设备会使事情爆炸),我必须使用 ctypes python 模块和原始 dll 函数从头开始工作WinUSB 和 SetupAPI。
我已经能够回答关于如何定义我传递给函数的结构的问题,但我现在遇到的问题是根据函数的规范......好吧, 我只是引用它。
"DeviceInterfaceData [out]
A pointer to a caller-allocated buffer that contains, on successful return, a
completed SP_DEVICE_INTERFACE_DATA structure that identifies an interface that meets
the search parameters. The caller must set DeviceInterfaceData.cbSize to
sizeof(SP_DEVICE_INTERFACE_DATA) before calling this function."
如何将这些要求转换为 Python?
我现在使用的结构类如下所示:
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(self)
self.ClassGuid = uuid.UUID(guid).get_bytes()
self.DevInst = (wintypes.DWORD)(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))
根据之前问题的答案告诉我。
谢谢。
【问题讨论】:
标签: python-2.7 ctypes windows-7-x64 setupapi pywinusb