PORTS Forum
Ports and Interfaces => USB => Topic started by: mihooper 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.
-
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.
-
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?
-
To detect a device that isn't attached on starting the application, create a timer that runs FindTheHid periodically.
-
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!!
-
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.
-
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?
-
After further review......
If you add a message test to OnDeviceChange, like this:
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!