0

我有以下代码来检测基于 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,
    }

我的问题是,似乎 Removal 事件似乎永远不会在 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?

4

1 回答 1

0

我只是通过修改我的 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 查看更新的驱动程序。

于 2013-10-21T16:31:12.460 回答