c - c中的原始输入winapi,无法获取设备信息

标签 c winapi

我正在摆弄 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是一个指针。你把它作为一个指针,但它没有指向任何东西。试试这个:

  1. 摆脱 pcbSize(到处)。
  2. 创建变量UINT cbDataSize = 1000。这是 pData 数组的大小。
  3. 对于 GetRawInputDeviceInfo 的最后一个参数,请使用 &cbDataSize。它获取cbDataSize的地址,该地址是一个指针。
  4. printf("%u\n", pcbSize); 更改为 printf("%u\n", cbDataSize);

看看这对您有何作用。

[编辑]

此外,您应该对 puiNumDevices 执行相同的操作。相反,创建一个名为 uiNumDevicesUINT。在函数需要指针的地方使用 &uiNumDevices

关于c - c中的原始输入winapi,无法获取设备信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5287816/

相关文章:

windows - 如何确定哪些任务栏应用程序/窗口正在请求用户注意

c - 用c编写的listSort函数工作错误

c++ - 如何使用cygwin编译ioping

c - 如何在没有 luarocks 的情况下编译 LuaFileSystem 库并获取 .dll 文件?

c - 如何在 C 中没有库函数(使用 read() 系统调用)的文件中找到单词?

c++ - Visual Studio 2013 : error C2039: 'SetDefaultDllDirectories' : is not a member of '` global namespace''

windows - FindFirstFile 多种文件类型

c - 为什么会产生段错误(核心转储)错误?

c++ - 我的函数操作 std::string 产生了意想不到的结果

c++ - 使用相同的窗口类多次调用 RegisterWindow 的副作用?