USB file structure

本文档详细介绍了Windows CE操作系统中USB驱动的USBD模块代码。USBD模块负责加载客户端驱动并为USB主机控制器提供接口。内容包括USBD与USB客户端驱动、HCD驱动之间的交互流程。

// Module Name:
//     CDevice.cpp
// Abstract:
//     This file manages the USB devices
//
//                  CDevice (ADT)
//                /               /
//            CFunction        CHub (ADT)
//                            /          /
//                        CRootHub   CExternalHub

 

 

Module Name:

usbddrv.cpp

Abstract:

This file contains code for the USBD module of the Universal Serial
Bus driver for Windows CE.  The USBD driver is responsible for loading
client drivers, and providing an interface to the USB host controller
to client drivers.

  +--------------+
  | USB Client   |
  |   Driver     |
  +--------------+
        /|/
         |       USBDI Interface
        /|/
  +--------------+
  |  USBD        |
  |  Driver      |
  +--------------+
        /|/
         |       HCD Interface
        /|/
  +--------------+
  |  HCD (OHCD or|
  |  UHCD) driver|
  +--------------+
        /|/
         |       HC Interface
        /|/
  +--------------------+               +------------------------+
  | Host controller HW |               | USB Device (function)  |
  +--------------------+               +------------------------+
        /|/                                        /|/
         |                                          |
         +------------------------------------------+

### Windows系统中使用Python读取USB设备插拔记录 在Windows系统中,可以利用`pywin32`库来监听和捕获USB设备的插拔事件。以下是实现这一目标的具体方法: #### 使用 `pywin32` 库监听 USB 插拔事件 通过调用 Windows 的消息机制,可以监控硬件变化的通知。具体来说,可以通过注册窗口过程函数(Window Procedure Function),接收来自操作系统的 WM_DEVICECHANGE 消息。 ```python import win32gui import win32con import time def on_device_change(hwnd, msg, wparam, lparam): """处理WM_DEVICECHANGE消息""" if msg == win32con.WM_DEVICECHANGE: if wparam == win32con.DBT_DEVNODES_CHANGED: # 设备状态改变 print("A USB device has been plugged/unplugged.") elif wparam == win32con.DBT_DEVICEARRIVAL: # 设备插入 print("A USB device was inserted.") elif wparam == win32con.DBT_DEVICEREMOVECOMPLETE: # 设备移除完成 print("A USB device was removed.") # 创建一个隐藏窗口用于接收消息 hwnd = win32gui.CreateWindow( win32con.WC_MESSAGE, "", 0, 0, 0, 0, 0, None, None, win32gui.GetModuleHandle(None), None) # 设置窗口的消息回调函数 win32gui.SetWindowLong(hwnd, win32con.GWL_WNDPROC, win32api.MakeProcInstance(on_device_change)) # 开始循环等待消息 while True: win32gui.PumpWaitingMessages() time.sleep(0.1) ``` 此代码片段实现了对 USB 设备插拔事件的监听[^4]。当有新的 USB 设备被插入或移除时,程序会打印相应的提示信息。 --- #### 结合 WMI 查询已连接的 USB 设备 如果需要进一步查询当前已经连接到计算机上的 USB 设备列表,则可以借助 Python 中的 `WMI` 模块执行更详细的查询操作。 ```python import wmi c = wmi.WMI() for usb in c.Win32_USBHub(): print(f"{usb.DeviceID} - {usb.Description}") ``` 这段脚本能够枚举所有挂载至系统的 USB 集线器及其子设备的信息[^5]。 --- #### 综合应用:实时监测并列出新接入的 USB 存储设备 为了更加贴近实际需求——即不仅检测到 USB 设备的变化还要识别具体的存储介质名称或者路径,下面提供了一个综合方案: ```python import win32file import win32con from ctypes import windll, byref, create_unicode_buffer, Structure, sizeof, pointer, c_wchar_p, c_uint32 class GUID(Structure): _fields_ = [("Data1", c_uint32), ("Data2", c_uint32), ("Data3", c_uint32), ("Data4", c_uint32 * 8)] def get_drive_letter_from_volume_guid(volume_name): buf_size = 64 buffer = create_unicode_buffer(buf_size) res = windll.kernel32.QueryDosDeviceW(c_wchar_p(volume_name.lstrip("\\??\\")), buffer, buf_size) if res != 0 and '\\\\.\\' in buffer.value: return buffer.value.split('\\')[0] return "" def main(): h_dev_notify = win32file.FindFirstChangeNotification(r"C:\", 0, win32con.FILE_NOTIFY_CHANGE_FILE_NAME | win32con.FILE_NOTIFY_CHANGE_DIR_NAME) try: old_drives = set([drive[:2] for drive in win32api.GetLogicalDriveStrings().split("\x00")[:-1]]) while True: result = win32file.WaitForSingleObject(h_dev_notify, win32event.INFINITE) new_drives = set([drive[:2] for drive in win32api.GetLogicalDriveStrings().split("\x00")[:-1]]) added_drives = list(new_drives.difference(old_drives)) removed_drives = list(old_drives.difference(new_drives)) if added_drives: print(f"Drives Added: {added_drives}") if removed_drives: print(f"Drives Removed: {removed_drives}") old_drives = new_drives win32file.FindNextChangeNotification(h_dev_notify) finally: win32file.FindCloseChangeNotification(h_dev_notify) if __name__ == "__main__": main() ``` 该示例展示了如何捕捉逻辑驱动器增减情况,并将其转换成易于理解的形式输出给用户[^6]。 --- ### 注意事项 - 上述解决方案适用于标准类型的 USB 外设;对于某些特殊用途的外接装置可能还需要额外配置权限或是安装专用驱动支持。 - 运行此类涉及底层访问的应用通常要求管理员特权,请确保运行环境满足条件后再尝试部署以上代码实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值