【问题标题】:USB Device Removal in Windows 8Windows 8 中的 USB 设备删除
【发布时间】:2012-06-14 09:22:28
【问题描述】:

我有以下代码来检测基于 Prolific Serial 的 USB 设备的到达和移除。

    protected override void WndProc(ref Message m)
    {
        DBT changeType = (DBT)m.WParam;
        string portName;

        if (m.Msg == WM_DEVICECHANGE)// && changeType != DBT.OTHER)
        {
            switch (changeType)
            {
                case DBT.DEVICEREMOVAL:
                    portName = Marshal.PtrToStringAuto((IntPtr)((long)m.LParam + 12));
                    // Device Removal
                    break;
                case DBT.DEVICEARRIVAL:
                    portName = Marshal.PtrToStringAuto((IntPtr)((long)m.LParam + 12));
                    // Device Arrival
                    break;
            }
        }

        base.WndProc(ref m);
    }

    enum DBT
    {
        DEVICEARRIVAL = 0x8000,
        DEVICEREMOVAL = 0x8004,
        OTHER = 0x0007,
    }

我的问题是在 Windows 8 中似乎永远不会发送 Removal 事件?此代码适用于 WinXp、Win2000、Vista 和 Win7,完美。

在插拔设备时监控消息后,Win8似乎从未传递正确的移除消息?

Win8下的结果如下:

检测:

HWnd=1770298, LParam=0, WParam=7, Result=0, Msg=537
HWnd=1770298, LParam=98820448, WParam=32768, Result=0, Msg=537
HWnd=1770298, LParam=0, WParam=7, Result=0, Msg=537

删除:

HWnd=1770298, LParam=0, WParam=7, Result=0, Msg=537

我的期望:

检测:

HWnd=590440, LParam=0, WParam=7, Result=0, Msg=537
HWnd=590440, LParam=1241588, WParam=32768, Result=0, Msg=537
HWnd=590440, LParam=0, WParam=7, Result=0, Msg=537

删除:

HWnd=590440, LParam=0, WParam=7, Result=0, Msg=537
HWnd=590440, LParam=1241588, WParam=32772, Result=0, Msg=537

最后一条消息在 Win8 中永远不会到达??

【问题讨论】:

  • 是否触发了任何事件? - 在你的 WmdProc 方法中添加一个 BP,看看是否有任何事情发生......
  • 我也遇到了同样的问题。 WM_DEVICECHANGE 即将到来,但只有代码 DBT_DEVNODES_CHANGED。谷歌搜索这个问题导致我这篇文章。我的问题是检测 USB 串行设备的到达和移除以安全地打开/关闭串行端口并分配/释放资源。有人说将 PretranslateMessage 替换为直接消息映射解决了 Vista/7 中的这个问题,但我无法为我的 Qt 5.1 项目检查它,因为我使用了覆盖的“nativeEvent”方法。

标签: c# winforms usb wndproc


【解决方案1】:

我只是通过修改我的 USB 串行驱动程序解决了这个问题。 通过正确配置在标准 usbser.sys 帮助上添加上层过滤器“serenum.sys”,因此问题消失了。

像这样的东西。来自生产代码的示例。

[Version]
Signature="$Windows NT$"
Class=Ports
ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
Provider=%MFGNAME%
LayoutFile=layout.inf
CatalogFile=%MFGFILENAME%.cat
DriverVer=08/29/2013,5.1.2600.10


[Manufacturer]
%MFGNAME%=DeviceList, NTamd64

[SourceDisksNames]

[DestinationDirs]
DefaultDestDir=12

;------------------------------------------------------------------------------
; Windows 2000/XP/Vista-32bit Sections
;------------------------------------------------------------------------------
[DriverInstall.nt]
include=mdmcpq.inf
CopyFiles=DriverCopyFiles.nt
AddReg=DriverInstall.nt.AddReg

[DriverInstall.nt.HW] 
AddReg=DriverInstall.nt.HW.AddReg 

[DriverInstall.nt.HW.AddReg]
HKR,,NTMPDriver,,*ntkern
HKR,,NTMPDriver,,usbser.sys
HKR,,PortSubClass, 0x00010000, "01"
HKR,,"UpperFilters",0x00010000,"serenum" 

[DriverCopyFiles.nt]
usbser.sys,,,0x20
serenum.sys,,,0x20

[DriverInstall.nt.AddReg]
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"

[DriverInstall.nt.Services]
AddService=usbser,  0x00000002, DriverService.nt
AddService=serenum, 0x00000000, SerenumService.nt

[DriverService.nt]
DisplayName="Driver name"
ServiceType=1
StartType=3
ErrorControl=1
ServiceBinary=%12%\%DRIVERFILENAME%.sys
LoadOrderGroup = Base

[SerenumService.nt]
DisplayName="Filter name"
ServiceType=1
StartType=3
ErrorControl=1
ServiceBinary=%12%\serenum.sys
LoadOrderGroup = PNP Filter

;------------------------------------------------------------------------------
; Vista-64bit Sections
;------------------------------------------------------------------------------

[DriverInstall.NTamd64]
include=mdmcpq.inf
CopyFiles=DriverCopyFiles.NTamd64
AddReg=DriverInstall.NTamd64.AddReg

[DriverCopyFiles.NTamd64]
usbser.sys,,,0x20
serenum.sys,,,0x20

[DriverInstall.NTamd64.AddReg]
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"

[DriverInstall.NTamd64.Services]
AddService=usbser, 0x00000002, DriverService.NTamd64
AddService=serenum, 0x00000000, SerenumService.NTamd64

[DriverService.NTamd64]
DisplayName="Driver display name"
ServiceType=1
StartType=3
ErrorControl=1
ServiceBinary=%12%\usbser.sys

[DriverInstall.NTamd64.HW] 
AddReg=DriverInstall.nt.HW.AddReg 

[DriverInstall.NTamd64.HW.AddReg]
HKR,,NTMPDriver,,*ntkern
HKR,,NTMPDriver,,usbser.sys
HKR,,PortSubClass, 0x00010000, "01"
HKR,,"UpperFilters",0x00010000,"serenum" 

[SerenumService.NTamd64]
DisplayName="Filter display name"
ServiceType=1
StartType=3
ErrorControl=1
ServiceBinary=%12%\serenum.sys

;------------------------------------------------------------------------------
; Vendor and Product ID Definitions
;------------------------------------------------------------------------------
; When developing your USB device, the VID and PID used in the PC side
; application program and the firmware on the microcontroller must match.
; Modify the below line to use your VID and PID. Use the format as shown below.
; Note: One INF file can be used for multiple devices with different VID and PIDs.
; For each supported device, append ",USB\VID_xxxx&PID_yyyy" to the end of the line.
;------------------------------------------------------------------------------
;[SourceDisksFiles]
;[SourceDisksNames]

[DeviceList]
%DESCRIPTION%=DriverInstall, USB\VID_XXXX&PID_XXXX

[DeviceList.NTamd64]
%DESCRIPTION%=DriverInstall, USB\VID_XXXX&PID_XXXX

;------------------------------------------------------------------------------
; String Definitions
;------------------------------------------------------------------------------
;Modify these strings to customize your device
;------------------------------------------------------------------------------
[Strings]
FullCompanyName="Your company name"
FullProductName="Your product name"
ShortProductName="Short name"
MFGFILENAME="FileName"
DRIVERFILENAME ="usbser"
MFGNAME="Manufacturer name"
INSTDISK="Installation disk"
DESCRIPTION="Description of the driver"
SERVICE="Name of the serenum service"

您可以查看 Prolific 驱动程序以找到此类部分。我也找到了它们,它们使用了 Upper Filter 和 Serenum,因此检测到设备插入成功。您也可以查看 Prolific 的更新驱动程序。

【讨论】:

  • serenum 会导致很多其他问题,例如 Windows 会自动为非鼠标设备加载鼠标驱动程序。我见过other USB-serial drivers that provide an "Event on Surprise Removal" configuration setting,所以这肯定在你自己的驱动程序中。
  • 谢谢你,本。我将为未来的设备编写自己的驱动程序。使用 USB CDC 可以让我们在 Windows、Linux 和 Mac OS X 上使用设备,而无需支付大量费用。 Linux 和 Mac OS 将我们的设备检测为“tty”,只有 Windows 需要驱动程序。
  • 基于“修改我的 USB 串行驱动程序”我以为你的意思是你写的。无论如何,serenum 很麻烦。
  • 编写我们的驱动程序是当前项目的未来计划。因此,这是提供 CDC 设备支持的最简单方法。一切正常,我们在错误检测和鼠标驱动程序加载方面没有遇到任何问题。但我相信 serenum 的代码可能有任何错误。
猜你喜欢
  • 2013-05-14
  • 1970-01-01
  • 2014-04-18
  • 1970-01-01
  • 2014-01-03
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多