Author Topic: Get PID/VID of already open device?  (Read 17821 times)

vanweric

  • Member
  • ***
  • Posts: 24
Get PID/VID of already open device?
« on: October 26, 2011, 03:19:47 pm »
I am developing an application that might have to co-exist with multiple copies of itself.  Each instance needs to be able to enumerate HID devices, but only one instance can connect to a given device at once.  I'm a bit confused on how the file handle permissions should be set.

How do I open a handle such other processes can read its PID/VID, but can not actually _use_ the device?

You can assume that the competing processes are well behaved (or at least both written with this in mind), but that they do not have any side-channel to talk to each other.

Thanks!
 - Eric

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Get PID/VID of already open device?
« Reply #1 on: October 26, 2011, 03:30:25 pm »
You can get the VID and PID with a handle opened without requesting read or write access.

See my example code on my HID page:

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

Jan

vanweric

  • Member
  • ***
  • Posts: 24
Re: Get PID/VID of already open device?
« Reply #2 on: October 26, 2011, 03:59:55 pm »
I took a look through two of the C# examples, but I was unable to find my answer - could you please point me a little closer?

In GenericHID it looks like FILE_SHARE_READ | FILE_SHARE_WRITE is set both when you open for getting PID/VID and when you open to actually communicate, but GENERIC_READ | GENERIC_WRITE is only set when you actually open to communicate:
Code: [Select]
hidHandle = FileIO.CreateFile(myDevicePathName, FileIO.GENERIC_READ | FileIO.GENERIC_WRITE, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);
hidHandle = FileIO.CreateFile(devicePathName[memberIndex], 0,  FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);

Wouldn't this allow another process to do a full open?

Thank you
 - Eric

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Get PID/VID of already open device?
« Reply #3 on: October 26, 2011, 04:34:49 pm »
Try it without FILE_SHARE_READ | FILE_SHARE_WRITE.

Jan

vanweric

  • Member
  • ***
  • Posts: 24
Re: Get PID/VID of already open device?
« Reply #4 on: October 26, 2011, 05:23:31 pm »
That does it!  Thanks Jan!

For the record, my call to check PID/VID etc is opened with
Code: [Select]
write_handle = CreateFileA(DevicePath,0,0,NULL,
OPEN_EXISTING,0,NULL);

and my call to start communication is
Code: [Select]
write_handle = CreateFileA(DevicePath,GENERIC_WRITE |GENERIC_READ,0,NULL,
OPEN_EXISTING,0,NULL);

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: Get PID/VID of already open device?
« Reply #5 on: October 27, 2011, 10:52:32 am »
That makes sense. Glad you got it working.

Jan