Windows 8中的USB设备删除

时间:2012-06-14 09:22:28

标签: c# winforms usb wndproc

我已经获得以下代码来检测到基于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中发送?此代码适用于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 ??

1 个答案:

答案 0 :(得分:0)

我只是通过修改usb-serial驱动程序解决了这个问题。 在标准的usbser.sys上添加Upper Filter'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检查更新的驱动程序。