C# Windows 8中USB设备的删除

C# Windows 8中USB设备的删除,c#,winforms,usb,wndproc,C#,Winforms,Usb,Wndproc,我有以下代码来检测一个多产的基于串行的USB设备的到达和移除 protected override void WndProc(ref Message m) { DBT changeType = (DBT)m.WParam; string portName; if (m.Msg == WM_DEVICECHANGE)// && changeType != DBT.OTHER) {

我有以下代码来检测一个多产的基于串行的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中发送?此代码适用于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=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=1770298, 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
我期待的是:

检测:

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=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=1770298, 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中出现???

我只需修改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"

你可以查看多产的驱动程序来找到这样的部分。我也找到了它们,它们使用了上过滤器和Serenum,所以设备插入检测成功。您还可以从Prolific检查更新的驱动程序。

是否触发了任何事件?-在你的WmdProc方法中加入一个BP,看看是否发生了任何事情……我也遇到了同样的问题。WM_设备更改即将到来,但仅在代码DBT_DEVNODES_更改的情况下。通过谷歌搜索这个问题,我找到了这个帖子。我的问题是检测USB串行设备的到达和移除,以安全地打开/关闭串行端口并分配/释放资源。有人告诉我,在Vista/7中,将PretranslateMessage替换为direct message map可以解决这个问题,但我无法检查我的Qt 5.1项目,因为我使用了覆盖的“nativeEvent”方法。
serenum
会导致许多其他问题,例如Windows自动为非鼠标设备加载鼠标驱动程序。我看到了,所以这肯定是由你自己的司机控制的。谢谢你,本。我将为未来的设备编写自己的驱动程序。使用USB CDC,我们可以在Windows、Linux和Mac OS X上使用该设备,而无需花费大量费用。Linux和Mac OS检测到我们的设备为“tty”,只有Windows需要驱动程序。基于“修改我的usb串行驱动程序”,我以为你是说你写的。无论如何,
serenum
是个麻烦。编写我们的驱动程序是当前项目的未来计划。所以这是提供CDC设备支持的最简单的方法。一切正常,我们不会遇到任何错误检测和鼠标驱动程序加载的问题。但我相信塞伦的密码可能有错误。