HidD_SetFeature - ERROR_INVALID_FUNCTION

Hi,

I’m trying to interact with an HID device with Windows API. I’m opening my device the following way:

handle = CreateFileA(path,
0,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,/*FILE_ATTRIBUTE_NORMAL,*/
0);

Then i’m trying to set a feature with the following code but it gives me an error:

unsigned char data[2] = { REPORTID_LED, value };

BOOL res = HidD_SetFeature(handle, (PVOID)data, 2);

if (!res)
{
int error = GetLastError(); // error: 1 - ERROR_INVALID_FUNCTION
}

My report descriptor is quite long as it describes a multitouch digitizer, so I will only past here the feature part:

0x09, 0x00, // USAGE (Undefined)
0xa1, 0x01, // COLLECTION (Application)
0x85, REPORTID_LED, // REPORT_ID (Feature)
0x09, 0x55, // USAGE(Maximum Count)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x07, // LOGICAL_MAXIMUM (7)
0x95, 0x01, // REPORT_COUNT (1)
0x75, 0x08, // REPORT_SIZE (8)
0xb1, 0x02, // FEATURE (Data,Var,Abs)
0xc0, // END_COLLECTION

This works under linux. I have tested before going to windows. Why is it falling in Windows? How can I know more about the ERROR_INVALID_FUNCTION?

Any ideas?

Thanks in advance,

Regards,

Nuno Santos

xxxxx@imaginando.net wrote:

Hi,

I’m trying to interact with an HID device with Windows API. I’m opening my device the following way:

handle = CreateFileA(path,
0,

You are opening the file but not requesting any permissions. So, you
get a handle, but you can’t read or write to it. SetFeature requires
write access. Replace this 0 with GENERIC_READ|GENERIC_WRITE.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Hi Tim,

Thanks for your quick reply. It worked indeed! :slight_smile:

I was using hidapi and I was having trouble opening the device.

Now I’m trying to read a feature but it takes some time to unblock the call and then fails

res = HidD_GetFeature(dev->device_handle, data, length);
if (!res)
{
int error = GetLastError(); // 121 - The semaphore timeout period has expired.
return -1;
}

Any ideas?

Regards

xxxxx@imaginando.net wrote:

I was using hidapi and I was having trouble opening the device.

Now I’m trying to read a feature but it takes some time to unblock the call and then fails

res = HidD_GetFeature(dev->device_handle, data, length);
if (!res)
{
int error = GetLastError(); // 121 - The semaphore timeout period has expired.
return -1;
}

Are you setting data[0] to the report ID immediately before calling this?


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Hi Tim,

Yes, I am.

unsigned char data[2];

data[0] = REPORTID_LED;

if (hid_get_feature_report(device, data, sizeof(data))==-1)
qDebug() << “failed to get feature value”;
else
qDebug() << data[0] << data[1];

Where hid_get_feature_report is:

int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
{
BOOL res;
#if 1
res = HidD_GetFeature(dev->device_handle, data, length);
if (!res) {
register_error(dev, “HidD_GetFeature”);
return -1;
}
return 0; /* HidD_GetFeature() doesn’t give us an actual length, unfortunately */
}

Any ideas?

Thx

Tim,

I forgot that I was not implementing that feature get report on the device side.

All working now! :slight_smile:

Thanks for your help.

Regards,

Nuno Santos