Question

I am trying to develop a C# application that can communicate with a USB HID. I have overriden my WndProc method so that is catches all of the WM_DEVICECHANGE events and passes the DeviceChange method to a method OnDeviceChange (this code is actually borrowed from Jan Axelson) which looks like this....

protected override void WndProc( ref Message m ) 
{            
    try 
    { 
        //  The OnDeviceChange routine processes WM_DEVICECHANGE messages.

        if ( m.Msg == DeviceManagement.WM_DEVICECHANGE ) 
        { 
            OnDeviceChange( m ); 
        } 

        //  Let the base form process the message.

        base.WndProc( ref m );                 
    } 
    catch ( Exception ex ) 
    { 
        DisplayException( this.Name, ex ); 
        throw ; 
    }             
} 

For some reason though, everytime I plug in a device, whether it be a mouse or a keyboard or the device I am developing, which are all HID's, the value of WParam is always 0x7;

I checked in DBT.h and the value of 0x0007 is ...

#define DBT_DEVNODES_CHANGED            0x0007

/*
 * Message = WM_DEVICECHANGE
 * wParam  = DBT_QUERYCHANGECONFIG
 * lParam  = 0
 *
 *      sent to ask if a config change is allowed
 */....

I don't just stop after the first message comes in either I look at all messages and for every one the value is always 0x0007. Why am I never seeing the DeviceAttached or DeviceRemoved events?

Anyone with some USB experience have any ideas?

Was it helpful?

Solution

You need to register your device in order to receive attached and removed. See RegisterDeviceNotification. Here is a great example to get you going.

OTHER TIPS

In a debugging session Visual Studio can omit messages.

I couldnt understand why both DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE were missed. Just unset breakpoint on line if ( m.Msg == DeviceManagement.WM_DEVICECHANGE ).

I have answered because post wasnt closed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top