locked
USB HID CreateFile and Write File RRS feed

  • Question

  • Hello All -

     

    I'll start by admitting that I am a novice working with Windows internals.  I have code that is sitting on top of the standard USB HID drivers and is trying to interact with an HID device (not mouse or keyboard).  I am able to successfully detect the device.  In the sequence of writing to the device, I'm invoking CreateFile() as such:

    HidHandle = CreateFile(
                        DeviceName,
                        GENERIC_READ | GENERIC_WRITE,
                        FILE_SHARE_READ | FILE_SHARE_WRITE,
                        0,
                        OPEN_EXISTING,
                        0,
                        0);

     

    The value of the only variable, "DeviceName", is:

                \\?\hid#vid_1cbe&pid_0000#6&116261e7&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}

    which I understand is the Device Path Name.

     

    The CreateFile() invocation succeeds and always returns what I assume to be a good value - a positive integer.  The handle is saved.

    The code then spawns another thread that opens the file for reading with the ReadFile() operation.  The call is:

                    int BytesRead = 0;
                    byte[] BufBytes = new byte[InputReportByteLength];
                    if (ReadFile(HidHandle, BufBytes, InputReportByteLength, ref BytesRead, null))

    I'm assuming this call blocks on a successful read because control is never returned.

    When I later try to send a message to the device using:

                    WriteFile(HidHandle, ref buffer, numberOfBytesToWrite, ref numberOfBytesWritten, 0);

    I never receive control back from the call.

    The "buffer" is a byte[64], and the "numberOfBytesToWrite" is 64, too.

     

    Can anyone shed some light about the ReadFile() and WriteFile() behavior?  Man, I'd love to see the APIs for the hid.dll, setupapi.dll and kernal32.dll.  Can anyone direct me to those descriptions.

     

    (What I wouldn't give for a UNIX man page;-)

     

    Thank you in advance.

    Monday, August 23, 2010 6:53 PM

Answers

  • I discovered that you cannot share file descriptors (handles) between threads, at least not the way I was using CreateFile().  I resolved the problem.
    Tuesday, August 24, 2010 7:29 PM

All replies

  • The control never returning from ReadFile or WriteFile isn't a good sign. Why do you need to read/write to the device directly ?

    Can you do an asynchronous IO to avoid the hang ? You may want to use ReadFileEx and WriteFileEx and cancel the IO if its not successful in certain time and retry again. Windows internally uses asynchronous IO even if you are doing a synchronous IO therefore you may want to explore above.

    You may also consider using IOCTL control codes instead of directly writing to the USB http://msdn.microsoft.com/en-us/library/ff563606(VS.85).aspx 

     

    Monday, August 23, 2010 10:08 PM
  • Based on all the code I have seen that interacts with a USB HID device, synchronous I/O is what is used, and the functions I have used are what is in the example code I am following.

    I need to correct myself on one fine point.  When reading/writing to a HID device there is a report identifier that must be added to the data stream at byte[0].  That means the actual byte[] length is 65.  I have corrected this, but still no-go.

    Thank you all, and please keep the comments coming.

    Regards.

    Tuesday, August 24, 2010 1:36 PM
  • I discovered that you cannot share file descriptors (handles) between threads, at least not the way I was using CreateFile().  I resolved the problem.
    Tuesday, August 24, 2010 7:29 PM
  • This isn't completly correct you can use the file handles across threads. Handles are process entity they can be shared in all threads.

    You can varify this by sharing the handle between threads while writing a file on the disk.

    Tuesday, August 24, 2010 10:49 PM
  • "I discovered that you cannot share file descriptors (handles) between threads, at least not the way I was using CreateFile(). I resolved the problem."

     

    Can you shed some more light on this one. As me new to VC++ facing the same problem while writing.

     

     

    HANDLE hFile = CreateFile(FileName, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

     

    Passing FileName as "\\\\.\\USB#Vid_04b8&Pid_0202#403336460002480000#{a5dcbf10-6530-11d2-901f-00c04fb951ed}" got it from registry directly. And, successfully getting the handle.

     

    WriteFile(hFile, Cmd_Print, 2, &bw, NULL); // here two bytes of data to write.

     

    Clarify me as a novice if I am doing anything wrong.

     

    Thanks,

     

    Anmol

    Saturday, May 28, 2011 8:48 AM