Author Topic: How to clear/refresh driver's HID device list  (Read 21243 times)

Dobaz

  • Member
  • ***
  • Posts: 23
How to clear/refresh driver's HID device list
« on: September 25, 2012, 03:42:32 pm »
Hello,

I have a soft that communicates continuously with a HID composite device.  The program can accept many of those devices to be connected and communicate with them one after the other. All is working fine but I get a soft crash when I disconnect two devices at the same time.

EX:
2 devices are connected
I remove them at the same time or almost
crash

What happens is that the first device disconnected is gone on the spot from the driver's list of HID device but the second one takes about 5 seconds.  SO I have to wait on place for 5 sec to avoid crash and then  check for devices connected to get the second one to be gone.

Is there any way to avoid this 5 secs, any way to clear/reset/refresh the driver'S HID device list on order?

I hope it is clear enough

Thanks

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: How to clear/refresh driver's HID device list
« Reply #1 on: September 25, 2012, 06:00:36 pm »
Do you get an error when it crashes?

Dobaz

  • Member
  • ***
  • Posts: 23
Re: How to clear/refresh driver's HID device list
« Reply #2 on: September 25, 2012, 06:17:11 pm »
Yes,

Unhandled exception at 0x00a5040a : 0xC0000005: Access violation reading location 0x00000003

I think I forgot to say that:

The problem is that both device are physically disconnected but when I get the number of device with GetHIDCollectionDevices, only once device is gone and the other one is still in the list for 5 secs. Since I still have a device in the list I continue to communicate with it with the information I have on this device but it's not there anymore.  So I wait 5 secs to wait until GetHIDCollectionDevices send the right number of device connected which is 0.

How to handle this without waiting 5 secs?

Thank you so


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: How to clear/refresh driver's HID device list
« Reply #3 on: September 25, 2012, 09:38:13 pm »
Are you checking for a valid handle before read or write (IsInvalid property in .NET)?

Dobaz

  • Member
  • ***
  • Posts: 23
Re: How to clear/refresh driver's HID device list
« Reply #4 on: September 26, 2012, 10:32:55 am »
Yes but maybe not the right way.

I use MFC C++... So I could not find the IsInvalid property.

In GetHidCollectionDevice, I have:

        DeviceHandle=CreateFile
         (aDetailData[HIDDeviceIndex]->DevicePath,
         0,
         FILE_SHARE_READ|FILE_SHARE_WRITE,
         (LPSECURITY_ATTRIBUTES)NULL,
         OPEN_EXISTING,
         0,
         NULL);

   if(DeviceHandle == INVALID_HANDLE_VALUE)
   {
      return GetLastError();
   }

I put a breakpoint on "return GetLastError();",  but it never breaks after I removed both device.  The second device is still in the list as if it was connected. I can handle the crash of the software but how can I detect that the device is gone, instead of having to wait 5 secs until the plug and play sound is heard?


In CreateFile, I use for OPEN_EXISTING for the parameter dwCreationDisposition [in],  I also tried CREATE_ALWAYS but it did not work. Could this be a problem?

Thanks

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: How to clear/refresh driver's HID device list
« Reply #5 on: September 26, 2012, 02:17:30 pm »
Using INVALID_HANDLE_VALUE should work.

Are you using RegisterDeviceNotification and OnDeviceChange to detect device removal?

Dobaz

  • Member
  • ***
  • Posts: 23
Re: How to clear/refresh driver's HID device list
« Reply #6 on: September 26, 2012, 10:07:16 pm »
No, I'm not.

I tired today but it was not easy to bring this in the program.  I may be wrong but aren't those functions suppose to work only if the devices handles are kept open?   I use CreateFile and then close the handle as soon as I am finished with the device.  So I don't keep it open. I did it this way because many devices can be connected at the same time, we can select it in a list and another application may use the device so I tried to avoid conflicts or...

Should I keep all the handles open instead?  Can I use RegisterDeviceNotification even if I keep opening and closing handles?

Thanks


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: How to clear/refresh driver's HID device list
« Reply #7 on: September 27, 2012, 10:42:11 am »
With RegisterDeviceNotification, an application can be notified of newly attached and removed devices.You don't need an open handle.

See my example code here:

http://www.lvr.com/hidpage.htm#MyExampleCode

Dobaz

  • Member
  • ***
  • Posts: 23
Re: How to clear/refresh driver's HID device list
« Reply #8 on: October 09, 2012, 03:12:26 pm »
Sorry!

Never too late to answer.  I was able to find a good solution for it,  the best case would have been to use "RegisterDeviceNotification" and I will test this.

Thank you very much