我正在开发适用于Windows和Mac的USB HID引导程序应用程序,我在开发应用程序时使用了LUFA库作为参考。我已经使用ARM芯片开发了该设备,现在正在使用主机应用程序。我的Mac主机应用程序正常运行,但我遇到了Windows主机应用程序的问题。
当我运行Windows主机应用时,我在调用HidD_SetFeature
和WriteFile
时无法获得有效的结果。但是,我可以从我的HID设备获取属性(HidD_GetAttributes
,HidD_GetSerialNumberString
,HidD_SetNumInputBuffers
,HidD_GetPreparsedData
,HidP_GetCaps
)。
我不确定为什么我的写入会继续返回0 / false但我通常会得到错误6或23.这6个错误代码令人困惑,因为那是ERROR_INVALID_HANDLE
但我能够获得所有其他属性使用相同的句柄。从HID库加载或者我是如何打开句柄的,我有什么不正确的做法吗?或者我的HID报告描述符很时髦?
谢谢!
我的加载功能:
typedef BOOLEAN(__stdcall *HidD_GetAttributes_)(HANDLE device, PHIDD_ATTRIBUTES attrib);
typedef BOOLEAN(__stdcall *HidD_GetSerialNumberString_)(HANDLE device, PVOID buffer, ULONG buffer_len);
typedef BOOLEAN(__stdcall *HidD_SetFeature_)(HANDLE handle, PVOID data, ULONG length);
typedef BOOLEAN(__stdcall *HidD_GetFeature_)(HANDLE handle, PVOID data, ULONG length);
typedef BOOLEAN(__stdcall *HidD_GetPreparsedData_)(HANDLE handle, PHIDP_PREPARSED_DATA *preparsed_data);
typedef BOOLEAN(__stdcall *HidD_FreePreparsedData_)(PHIDP_PREPARSED_DATA preparsed_data);
typedef NTSTATUS(__stdcall *HidP_GetCaps_)(PHIDP_PREPARSED_DATA preparsed_data, HIDP_CAPS *caps);
typedef BOOLEAN(__stdcall *HidD_SetNumInputBuffers_)(HANDLE handle, ULONG number_buffers);
static HidD_GetAttributes_ HidD_GetAttributes;
static HidD_GetSerialNumberString_ HidD_GetSerialNumberString;
static HidD_SetFeature_ HidD_SetFeature;
static HidD_GetFeature_ HidD_GetFeature;
static HidD_GetPreparsedData_ HidD_GetPreparsedData;
static HidD_FreePreparsedData_ HidD_FreePreparsedData;
static HidP_GetCaps_ HidP_GetCaps;
static HidD_SetNumInputBuffers_ HidD_SetNumInputBuffers;
void load_library_functions() {
lib_handle = LoadLibraryA("hid.dll");
HidD_GetAttributes = (HidD_GetAttributes_)(GetProcAddress(lib_handle, "HidD_GetAttributes");
HidD_GetSerialNumberString = (HidD_GetSerialNumberString_)(GetProcAddress(lib_handle, "HidD_GetSerialNumberString");
HidD_SetFeature = (HidD_SetFeature_)(GetProcAddress(lib_handle, "HidD_SetFeature");
HidD_GetFeature = (HidD_GetFeature_)(GetProcAddress(lib_handle, "HidD_GetFeature");
HidD_GetPreparsedData = (HidD_GetPreparsedData_)(GetProcAddress(lib_handle, "HidD_GetPreparsedData");
HidD_FreePreparsedData = (HidD_FreePreparsedData_)(GetProcAddress(lib_handle, "HidD_FreePreparsedData");
HidP_GetCaps = (HidP_GetCaps_)(GetProcAddress(lib_handle, "HidP_GetCaps");
HidD_SetNumInputBuffers = (HidD_SetNumInputBuffers_)(GetProcAddress(lib_handle, "HidD_SetNumInputBuffers");
}
以及我如何打开设备的句柄:
// Open handle to device
Handle write_handle = CreateFile(device_interface_detail_data->DevicePath,
(GENERIC_READ | GENERIC_WRITE),
(FILE_SHARE_READ | FILE_SHARE_WRITE),
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,/*FILE_ATTRIBUTE_NORMAL,*/
NULL);
由于HID报告描述符出现奇怪的事情:
{
// Undefined user page
0x06, 0x00, 0xFF,
// Usage (vendor defined)
0x09, 0x01,
// Collection Start
0xA1, 0x01,
// Usage (2)
0x09, 0x02,
// Logical Minimum, 0
0x14, 0x00,
// Logical Maximum, -1
0x24, 0xFF,
// Report Size, 8 bits
0x75, 0x08,
// Report Count, 64 fields
0x95, 0x40,
// FEATURE definition
0xB1, 0x02
// Usage (3)
0x09, 0x03,
// Logical Minimum, 0
0x14, 0x00,
// Logical Maximum, -1
0x24, 0xFF,
// Report Size, 8 bits
0x75, 0x08,
// Report Count, 64 fields
0x95, 0x40,
// INPUT definition
0x81, 0x02,
// Usage (4)
0x09, 0x04,
// Logical Minimum, 0
0x14, 0x00,
// Logical Maximum, -1
0x24, 0xFF,
// Report Size, 8 bits
0x75, 0x08,
// Report Count, 256 fields
0x96, 0x00, 0x10
// OUTPUT definition
0x91, 0x02,
// End Collection
0xC1
}