GetRawInputDeviceInfo返回Windows 10中USB HID设备名称的错误语法

时间:2018-12-05 12:32:55

标签: windows winapi visual-c++

我有一个在互联网上找到的使用功能GetRawInputDeviceInfo的代码,但没有正确获得设备的名称。有时根本没有名字。我一直在寻找答案,发现人们在Windows XP和Windows 7上遇到此问题。我正在使用Windows 10,因此对我没有太大帮助。

C++ - WinAPI get list of all connected USB devices(我需要发布代码本身吗?是堆栈溢出的新消息)

最后,我要做的是获取连接到PC的所有设备的名称并打印出来,但是此功能也不会返回鼠标的名称,因此,如果有人提出建议,有关如何解决该问题的方法,或者一种更好的方法来获得自己喜欢的名字,以听听您的想法。在此先感谢-shon:)

EDIT2!完整代码:

#include <windows.h>
#include <iostream>
#include <vector>
#include <string>
#include <set>

// Namespace
using namespace std;

// Main
int main()
{
    // Program
    cout << "USB Device Lister." << endl;

    // Get Number Of Devices
    UINT nDevices = 0;
    GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST));

    // Got Any?
    if (nDevices < 1)
    {
        // Exit
        cout << "ERR: 0 Devices?";
        cin.get();
        return 0;
    }

    // Allocate Memory For Device List
    PRAWINPUTDEVICELIST pRawInputDeviceList;
    pRawInputDeviceList = new RAWINPUTDEVICELIST[sizeof(RAWINPUTDEVICELIST) * nDevices];

    // Got Memory?
    if (pRawInputDeviceList == NULL)
    {
        // Error
        cout << "ERR: Could not allocate memory for Device List.";
        cin.get();
        return 0;
    }

    // Fill Device List Buffer
    int nResult;
    nResult = GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST));

    // Got Device List?
    if (nResult < 0)
    {
        // Clean Up
        delete[] pRawInputDeviceList;

        // Error
        cout << "ERR: Could not get device list.";
        cin.get();
        return 0;
    }

    std::set<std::string> DeviceList;
    // Loop Through Device List
    for (UINT i = 0; i < nDevices; i++)
    {
        // Get Character Count For Device Name
        UINT nBufferSize = 0;
        nResult = GetRawInputDeviceInfo(pRawInputDeviceList[i].hDevice, // Device
            RIDI_DEVICENAME,                // Get Device Name
            NULL,                           // NO Buff, Want Count!
            &nBufferSize);                 // Char Count Here!

                                           // Got Device Name?
        if (nResult < 0)
        {
            // Error
            cout << "ERR: Unable to get Device Name character count.. Moving to next device." << endl << endl;

            // Next
            continue;
        }

        // Allocate Memory For Device Name
        WCHAR* wcDeviceName = new WCHAR[nBufferSize + 1];

        // Got Memory
        if (wcDeviceName == NULL)
        {
            // Error
            cout << "ERR: Unable to allocate memory for Device Name.. Moving to next device." << endl << endl;

            // Next
            continue;
        }

        // Get Name
        nResult = GetRawInputDeviceInfo(pRawInputDeviceList[i].hDevice, // Device
            RIDI_DEVICENAME,                // Get Device Name
            wcDeviceName,                   // Get Name!
            &nBufferSize);                 // Char Count

                                           // Got Device Name?
        if (nResult < 0)
        {
            // Error
            cout << "ERR: Unable to get Device Name.. Moving to next device." << endl << endl;

            // Clean Up
            delete[] wcDeviceName;

            // Next
            continue;
        }

        // Set Device Info & Buffer Size
        RID_DEVICE_INFO rdiDeviceInfo;
        rdiDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO);
        nBufferSize = rdiDeviceInfo.cbSize;

        // Get Device Info
        nResult = GetRawInputDeviceInfo(pRawInputDeviceList[i].hDevice,
            RIDI_DEVICEINFO,
            &rdiDeviceInfo,
            &nBufferSize);

        // Got All Buffer?
        if (nResult < 0)
        {
            // Error
            cout << "ERR: Unable to read Device Info.. Moving to next device." << endl << endl;

            // Next
            continue;
        }

        // Mouse
        if (rdiDeviceInfo.dwType == RIM_TYPEMOUSE)
        {
            // Current Device
            int id = rdiDeviceInfo.mouse.dwId; //device id
            string s = "ID: " + std::to_string(id) + ", Type : MOUSE"; //device type is mouse
            DeviceList.insert(s);
        }

        // Keyboard
        else if (rdiDeviceInfo.dwType == RIM_TYPEKEYBOARD)
        {
        // Current Device
        cout << endl << "Displaying device " << i + 1 << " information. (KEYBOARD)" << endl;
        wcout << L"Name " << wcDeviceName << endl; //*Problem is here!* //
        cout << "Keyboard mode: " << rdiDeviceInfo.keyboard.dwKeyboardMode << endl;
        cout << "Number of function keys: " << rdiDeviceInfo.keyboard.dwNumberOfFunctionKeys << endl;
        cout << "Number of indicators: " << rdiDeviceInfo.keyboard.dwNumberOfIndicators << endl;
        cout << "Number of keys total: " << rdiDeviceInfo.keyboard.dwNumberOfKeysTotal << endl;
        cout << "Type of the keyboard: " << rdiDeviceInfo.keyboard.dwType << endl;
        cout << "Subtype of the keyboard: " << rdiDeviceInfo.keyboard.dwSubType << endl;
        }

        // Some HID
        else // (rdi.dwType == RIM_TYPEHID)
        {
        // Current Device
        cout << endl << "Displaying device " << i + 1 << " information. (HID)" << endl;
        wcout << L"Device Name: " << wcDeviceName << endl;
        cout << "Vendor Id:" << rdiDeviceInfo.hid.dwVendorId << endl;
        cout << "Product Id:" << rdiDeviceInfo.hid.dwProductId << endl;
        cout << "Version No:" << rdiDeviceInfo.hid.dwVersionNumber << endl;
        cout << "Usage for the device: " << rdiDeviceInfo.hid.usUsage << endl;
        cout << "Usage Page for the device: " << rdiDeviceInfo.hid.usUsagePage << endl;
        } 

        // Delete Name Memory!
        delete[] wcDeviceName;
    }

    // Clean Up - Free Memory
    delete[] pRawInputDeviceList;

    for (std::set<string>::iterator i = DeviceList.begin(); i != DeviceList.end(); ++i)
        std::cout << *i << '\n';
    // Exit
    cout << endl << "Finnished.";
    cin.get();
    return 0;
}

1 个答案:

答案 0 :(得分:0)

在Windows中,有两种API调用:Unicode和ANSI。前者接收并返回UTF-16编码的Unicode字符串;后者接受并返回8位编码的字符串(确切的编码取决于操作系统的本地化)。

通过#定义(或不​​#定义)宏UNICODE选择要使用的风味。取决于函数更改名称的后缀为WA

#ifdef UNICODE
#define GetRawInputDeviceInfo GetRawInputDeviceInfoW
#else
#define GetRawInputDeviceInfo GetRawInputDeviceInfoA
#endif

所有可能包含文本数据的结构也都使用后缀WA复制。

现在,您的问题是:您没有定义UNICODE,因此实际上是在调用GetRawInputDeviceInfoA()(一种需要char*的ANSI风格),但是您正在传递WCHAR* ,这是一个UNICODE字符串!

解决方案很简单:

char* wcDeviceName = new char[nBufferSize + 1];

不幸的是,此函数GetRawInputDeviceInfo()的参数已重载,因此它被声明为采用void*,因此编译器无法捕获该错误。如果您要调用一个简单的函数,请说SetWindowText(),则由于指针类型不兼容,您将遇到编译器错误。

如果您确实想要设备的完整UNICODE名称,则可能更喜欢保留WCHAR字符串,然后专门调用UNICODE函数:

WCHAR* wcDeviceName = new WCHAR[nBufferSize + 1];
...
GetRawInputDeviceInfoW(..., RIDI_DEVICENAME, wcDeviceName, ...);