简体   繁体   中英

USB Device Removal in Windows 8

I've got the following code the detect the arrival and removal of a Prolific Serial based USB Device.

    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,
    }

My problem is that it seems as if the Removal event never gets sent in Windows 8? This code works for WinXp, Win2000, Vista & Win7, perfectly.

After monitoring the messages while plugging and removing the device, it seems as if Win8 never passes the correct removal message?

Here are the results under Win8:

Detection:

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

Removal:

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

What I'm expecting:

Detection:

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

Removal:

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

This last message never arrives in Win8??

I have solved this problem simply by modifying my usb-serial driver. Adding Upper Filter 'serenum.sys' over standard usbser.sys help with proper configuration, so the problem disappeared.

Some thing like this. Example from production code.

[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"

You could check Prolific driver to find such sections. As well i have found them and they use Upper Filter and Serenum, so device insertion detected successful. Also you could check newer driver from Prolific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM