none
IOCTL_HID_* from User Mode Application? RRS feed

  • Question

  • Hello All,

    I have created a hardware device which reports as a USB HID device to Windows. Currently it reports as a HID joystick with force feedback support (it fully works).

    I have now started to write a desktop application to use for administration of the device. What I would like to do is to read and write HID reports and features to and from the device.

    After reading the msdn documentation, the simplest way for me to do that seems to be to use IOCTLs.

    As initial test I have taken the hclient sample application (http://code.msdn.microsoft.com/windowshardware/HClient-HID-Sample-4ec99697) and modified it to call DeviceIoControl() with IOCTL_HID_GET_INPUT_REPORT.

    It however does not work and GetLastError() returns ERROR_GEN_FAILURE (0x1F).

    Before digging more into this, is this expected behavior, or should it work? I am on Win7 pro 64 bit

    Here is my test code (in function OpenHidDevice() in pnp.c)

    HidDevice->HidDevice = CreateFile (DevicePath, accessFlags, sharingFlags, NULL, // no SECURITY_ATTRIBUTES structure OPEN_EXISTING, // No special create flags 0, // Open device as non-overlapped so we can get data NULL); // No template file

    if (INVALID_HANDLE_VALUE == HidDevice->HidDevice) 
        {
            free(HidDevice -> DevicePath);
            HidDevice -> DevicePath = NULL ;
            return FALSE;
        }

    if (strstr(DevicePath, "vid_0925&pid_9006") != NULL)
    {
    BOOL success;
    DWORD outputBufferSize = 16;
    UCHAR inputReportBuffer[16];
    DWORD nBytesReturned = 0;
    DWORD lastError = 0;

    memset(inputReportBuffer, 0, (size_t)outputBufferSize);
    inputReportBuffer[0] = 0x01;

    success = DeviceIoControl(HidDevice->HidDevice,
    IOCTL_HID_GET_INPUT_REPORT,
    NULL,
    0,
    inputReportBuffer,
    outputBufferSize,
    &nBytesReturned,
    NULL);

    if (!success)
    {
    lastError = GetLastError();
    }

    ...
    }

    ...

    Saturday, May 25, 2013 10:34 PM

Answers

  • anything under the "HID Minidrivers IOCTLs" topic is for kernel mode only for hidclass to send to the miniport, not for an app. everything under "HID Class Driver IOCTLs" is where you can you send an IOCTL, from user mode, http://msdn.microsoft.com/en-us/library/windows/hardware/ff539849(v=vs.85).aspx

    > if (strstr(DevicePath, "vid_0925&pid_9006")

    the device path is opaque, you are not allowed to parse it. besides, this information is readily available to you via IOCTL_HID_GET_COLLECTION_INFORMATION / HID_COLLECTION_INFORMATION and the VendorID and ProductID fields, http://msdn.microsoft.com/en-us/library/windows/hardware/ff539870(v=vs.85).aspx


    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Saturday, May 25, 2013 11:39 PM
  • Hello again,

    I have worked some more with this, and IOCTL_HID_GET_INPUT_REPORT and HidD_GetInputReport() do not work (error 0x1F) with with my device described above, with my device reporting as a simple two axis, two button HID joystick or with a Microsoft joystick I own.

    It turned out I had to use ReadFile() to be able to read out input report data from the joysticks, which is quite inconvenient with complicated report descriptors and the parsing you have to perform - but at least you can get the data out that way.

    Thanks,

    • Marked as answer by Px_seven Monday, May 27, 2013 2:41 PM
    Monday, May 27, 2013 2:41 PM

All replies

  • anything under the "HID Minidrivers IOCTLs" topic is for kernel mode only for hidclass to send to the miniport, not for an app. everything under "HID Class Driver IOCTLs" is where you can you send an IOCTL, from user mode, http://msdn.microsoft.com/en-us/library/windows/hardware/ff539849(v=vs.85).aspx

    > if (strstr(DevicePath, "vid_0925&pid_9006")

    the device path is opaque, you are not allowed to parse it. besides, this information is readily available to you via IOCTL_HID_GET_COLLECTION_INFORMATION / HID_COLLECTION_INFORMATION and the VendorID and ProductID fields, http://msdn.microsoft.com/en-us/library/windows/hardware/ff539870(v=vs.85).aspx


    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Saturday, May 25, 2013 11:39 PM
  • anything under the "HID Minidrivers IOCTLs" topic is for kernel mode only for hidclass to send to the miniport, not for an app. everything under "HID Class Driver IOCTLs" is where you can you send an IOCTL, from user mode, http://msdn.microsoft.com/en-us/library/windows/hardware/ff539849(v=vs.85).aspx

    Yes, and the IOCTL_HID_GET_INPUT_REPORT is from the "HID Class Driver IOCTLs" set, so it should be fine to use from an application.

    From your answer, what I am trying to do should work then?

    Saturday, May 25, 2013 11:51 PM
  • The docs for HidD_GetInputReport are more explicit about the format of the output buffer, http://msdn.microsoft.com/en-us/library/windows/hardware/ff538945(v=vs.85).aspx.

    you must set the first byte in the output buffer to the report id if your TLC contains report ids


    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Sunday, May 26, 2013 1:54 AM
  • Hello again,

    I have worked some more with this, and IOCTL_HID_GET_INPUT_REPORT and HidD_GetInputReport() do not work (error 0x1F) with with my device described above, with my device reporting as a simple two axis, two button HID joystick or with a Microsoft joystick I own.

    It turned out I had to use ReadFile() to be able to read out input report data from the joysticks, which is quite inconvenient with complicated report descriptors and the parsing you have to perform - but at least you can get the data out that way.

    Thanks,

    • Marked as answer by Px_seven Monday, May 27, 2013 2:41 PM
    Monday, May 27, 2013 2:41 PM
  • Hi Px_seven and all,

    Would you please explain more how did you resolve this.

    I am facing the same problem.

    I am using the same hclient sample.

    Readfil fails with acces denied code if I do not call createfile with generic_read.

    CreateFile fails if I use genereic_read with error code error_sharing_voilation.

    Hidd_getinputreport fails with error_gen_failure by calling createfile(devicepath, 0, .....)

    Friday, February 6, 2015 10:42 AM