|
- #include <windows.h>
- #include <iostream>
- #include <string>
- #include <vector>
- // 结构体用于存储鼠标信息
- struct MouseInfo {
- std::wstring name;
- std::wstring deviceId; // 可以看作类似序号的标识
- };
- // 比较函数用于排序鼠标设备列表
- bool CompareMouseInfo(const MouseInfo& m1, const MouseInfo& m2) {
- return m1.deviceId < m2.deviceId;
- }
- // 从设备路径中提取设备名称
- std::wstring ExtractMouseName(const std::wstring& devicePath) {
- size_t startIndex = devicePath.find_last_of(L'\\') + 1;
- size_t endIndex = devicePath.find_last_of(L'#');
- if (endIndex == std::wstring::npos) {
- endIndex = devicePath.length();
- }
- return devicePath.substr(startIndex, endIndex - startIndex);
- }
- int main() {
- // 存储鼠标信息的向量
- std::vector<MouseInfo> mice;
- // 获取系统中所有原始输入设备的数量
- UINT numDevices;
- GetRawInputDeviceList(NULL, &numDevices, sizeof(RAWINPUTDEVICELIST));
- if (numDevices > 0) {
- PRAWINPUTDEVICELIST deviceList = new RAWINPUTDEVICELIST[numDevices];
- if (GetRawInputDeviceList(deviceList, &numDevices, sizeof(RAWINPUTDEVICELIST)) > 0) {
- for (UINT i = 0; i < numDevices; i++) {
- if (deviceList[i].dwType == RIM_TYPEMOUSE) {
- UINT deviceNameSize;
- GetRawInputDeviceInfoW(deviceList[i].hDevice, RIDI_DEVICENAME, NULL, &deviceNameSize);
- WCHAR* deviceName = new WCHAR[deviceNameSize];
- if (GetRawInputDeviceInfoW(deviceList[i].hDevice, RIDI_DEVICENAME, deviceName, &deviceNameSize) > 0) {
- MouseInfo mouse;
- mouse.name = ExtractMouseName(deviceName);
- mouse.deviceId = deviceName;
- mice.push_back(mouse);
- }
- delete[] deviceName;
- }
- }
- }
- delete[] deviceList;
- }
- // 对鼠标信息进行排序(可选,便于查看)
- std::sort(mice.begin(), mice.end(), CompareMouseInfo);
- // 输出鼠标信息
- for (const auto& mouse : mice) {
- std::wcout << "Mouse Name: " << mouse.name << std::endl;
- std::wcout << "Mouse Device ID (similar to serial number): " << mouse.deviceId << std::endl;
- }
- return 0;
- }
复制代码
|
|