我正在摆弄 USB RFID 扫描仪并尝试使用 raw input 读取输入,到目前为止我有这个
#define _WIN32_WINNT 0x0601
#include <windows.h>
#include <stdio.h>
int main(void)
{
PRAWINPUTDEVICELIST pRawInputDeviceList;
PUINT puiNumDevices, pcbSize;
UINT cbSize = sizeof(RAWINPUTDEVICELIST);
char *pData[1000];
GetRawInputDeviceList(NULL, puiNumDevices, cbSize);
pRawInputDeviceList = malloc(cbSize * *puiNumDevices);
GetRawInputDeviceList(pRawInputDeviceList, puiNumDevices, cbSize);
// gives a correct RIM_TYPE for all devices 0-7 (GetRawInputDeviceList returns 8 devices for me)
printf("%I32u\n", pRawInputDeviceList[0].dwType);
GetRawInputDeviceInfo(pRawInputDeviceList[1].hDevice, RIDI_DEVICENAME, pData, pcbSize);
// gives a huge number (garbage?), should be the length of the name
printf("%u\n", pcbSize);
// "E" in my case
printf("%s\n", pData);
// error 87, apparently ERROR_INVALID_PARAMETER
printf("%I32u\n", GetLastError());
return 0;
}
最佳答案
当您调用GetRawInputDeviceInfo
时,它期望pcbSize
是一个指针。你把它作为一个指针,但它没有指向任何东西。试试这个:
- 摆脱
pcbSize
(到处)。 - 创建变量
UINT cbDataSize = 1000
。这是pData
数组的大小。 - 对于
GetRawInputDeviceInfo
的最后一个参数,请使用&cbDataSize
。它获取cbDataSize
的地址,该地址是一个指针。 - 将
printf("%u\n", pcbSize);
更改为printf("%u\n", cbDataSize);
。
看看这对您有何作用。
[编辑]
此外,您应该对 puiNumDevices
执行相同的操作。相反,创建一个名为 uiNumDevices
的 UINT
。在函数需要指针的地方使用 &uiNumDevices
。
关于c - c中的原始输入winapi,无法获取设备信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5287816/