Raw input winapi in c, can't get device info
I'm messing around with a USB RFID scanner and trying to read input with raw input, so far I have this
#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(pRawInp开发者_如何学GoutDeviceList, 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;
}
When you call GetRawInputDeviceInfo
, it expects pcbSize
to be a pointer. You have it as a pointer, but its not pointing to anything. Try this:
- Get rid of
pcbSize
(everywhere). - Create a variable
UINT cbDataSize = 1000
. This is the size of yourpData
array. - For the last argument of
GetRawInputDeviceInfo
, use&cbDataSize
. This takes the address ofcbDataSize
, the address is a pointer. - Change
printf("%u\n", pcbSize);
toprintf("%u\n", cbDataSize);
.
See how that works for you.
[edit]
Also, you should do the same thing for puiNumDevices
. Instead, create a UINT
called uiNumDevices
. Use &uiNumDevices
where the functions expect pointers.
I am going to go out on a limb here and guess that this thing may actually be a HID device. Do you know if it is?
HID Devices are actually pretty easy to talk to; you connect to them via CreateFile() -- the same way that you would open a COM port -- and then just ReadFile() to get data.
Most of the problem is figuring out the correct path to connect to. It's actually a value called DevicePath that you get from SetupDiGetDeviceInterfaceDetail().
A rough map of it looks like this:
HidD_GetHidGuid() to get the HID guid
SetupDiGetClassDevs() to get the list of dev
Looping through devs, until you find yours:
- SetupDiEnumDeviceInterfaces() to enum the device interfaces
- SetupDiGetDeviceInterfaceDetail() to get the detail
- CreateFile() to open the device with the DevicePath from the detail.
- HidD_GetAttributes to get the vendorid and productid to see if it's your device.
If it is, remember it, and use ReadFile() to get data.
精彩评论