Author Topic: Detecting a newly attached HID?  (Read 17795 times)

mihooper

  • Member
  • ***
  • Posts: 24
Detecting a newly attached HID?
« on: December 28, 2012, 10:21:55 am »
Jan,

I am using a modified version of Generic HID (GH) in my application. In GH the attachment process is manual, which means the HID device is first attached, then you manually search for the device by clicking a button. In my application I would like to automatically detect a newly attached HID device and determine whether it is my VID/PID after it is connected. However, I am a bit confused as to how the WM_DEVICECHANGE messaging works. It appears as though I must first register my device for notifications before I receive a WM_DEVICECHANGE message.

Is there a way to be notified of *any* HID device attachments so that I can then determine whether it is my HID? I am a little vague on how this messaging and registration works.
Thx,
MikeH

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Detecting a newly attached HID?
« Reply #1 on: December 28, 2012, 11:31:10 am »
The RegisterForDeviceNotifications routine will cause any HID to be detected if you use the HID Guid (which my application does).

The OnDeviceChange routine examines the detected HID to see if it's the desired device.

mihooper

  • Member
  • ***
  • Posts: 24
Re: Detecting a newly attached HID?
« Reply #2 on: December 28, 2012, 05:11:36 pm »
Jan,

Thanks for the reply, but I'm afraid I may not have been clear on what I want to do.

The way I believe Generic HID works is:

1. Search for any HID device to see if at least one HID is connected.
2. If at least one HID is connected, search all connected HID devices and compare them to the VID/PID entered into the text boxes.
3. If my HID device is found (matches VID/PID), register it for notifications so that you can detect removal or re-attachment.

However, I would like to have some way to detect the attachment of my HID device without searching for it, and then registering it for notifications. Unless I am misunderstanding something, Generic HID requires the user to first manually search, then register a device, before any notifications are generated. Is this correct?
Thx,
MikeH

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Detecting a newly attached HID?
« Reply #3 on: December 28, 2012, 09:44:46 pm »
To detect a device that isn't attached on starting the application, create a timer that runs FindTheHid periodically.


mihooper

  • Member
  • ***
  • Posts: 24
Re: Detecting a newly attached HID?
« Reply #4 on: December 29, 2012, 09:30:42 am »
Thanks Jan. That's exactly what I had done, but wasn't sure if that was the "correct" way.

Thanks again for all of the great info!!
Thx,
MikeH

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Detecting a newly attached HID?
« Reply #5 on: March 11, 2013, 12:58:09 pm »
I wasn't thinking when I wrote my previous response. In my GenericHid example, edit the OnDeviceChange routine to run FindTheHid if the device wasn't previously detected. No need to use a timer.

mihooper

  • Member
  • ***
  • Posts: 24
Re: Detecting a newly attached HID?
« Reply #6 on: October 31, 2015, 05:08:58 pm »
Jan,

I would like to dust off this old thread and ask again in a different way.

I would like to detect the attachment (arrival) of a previously unregistered HID. Should the OnDeviceChange method do this? Or does it only respond to the removal (or arrival) of a currently registered device?
Thx,
MikeH

mihooper

  • Member
  • ***
  • Posts: 24
Re: Detecting a newly attached HID?
« Reply #7 on: November 01, 2015, 09:48:27 am »
After further review......

If you add a message test to OnDeviceChange, like this:
Code: [Select]
       internal void OnDeviceChange(Message m)
        {
            try
            {
                switch (m.WParam.ToInt32())
                {
                    case DeviceManagement.DBT_DEVICEARRIVAL:
...you can capture the device arrival and decide if it is your device by using FindHIDdevices. No timer necessary!
Thx,
MikeH