Ports
May 23, 2013, 03:02:13 pm *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: SMF - Just Installed!
 
   Home   Help Search Login Register  
Welcome to the PORTS Forum. Please post your questions, answers, and comments relating to USB and other hardware interfaces in the appropriate forums below. - Jan
Pages: [1]
  Print  
Author Topic: Get PID/VID of already open device?  (Read 1403 times)
vanweric
Member
***
Posts: 24


« on: October 26, 2011, 02: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
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1629


WWW
« Reply #1 on: October 26, 2011, 02: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
Logged
vanweric
Member
***
Posts: 24


« Reply #2 on: October 26, 2011, 02: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:
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
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1629


WWW
« Reply #3 on: October 26, 2011, 03:34:49 pm »

Try it without FILE_SHARE_READ | FILE_SHARE_WRITE.

Jan
Logged
vanweric
Member
***
Posts: 24


« Reply #4 on: October 26, 2011, 04:23:31 pm »

That does it!  Thanks Jan!

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

and my call to start communication is
Code:
write_handle = CreateFileA(DevicePath,GENERIC_WRITE |GENERIC_READ,0,NULL,
OPEN_EXISTING,0,NULL);
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1629


WWW
« Reply #5 on: October 27, 2011, 09:52:32 am »

That makes sense. Glad you got it working.

Jan
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!