如何在 C# 中获取原始输入设备的正确显示名称?

问题描述 投票:0回答:1

我正在制作一个简单的程序来枚举当前连接到 Windows 的所有设备及其指针,然后显示您在 C# 中单击 WPF 窗口上的按钮的设备的正确名称。

我已经完成了收集所有设备输入的代码

NativeMethods.GetRawInputDeviceList(pRawInputDeviceList, ref deviceCount, (uint)dwSize);

for (var i = 0; i < deviceCount; i++)
{
   var rid = (RawInputDeviceList)Marshal.PtrToStructure(new IntPtr((pRawInputDeviceList.ToInt64() + (dwSize * i))), typeof(RawInputDeviceList));
}

现在我需要的只是获取设备的产品名称,对于我的鼠标来说,又名 TUF GAMING M5,这就是它在我的设备管理器窗口中显示的方式。

看起来好像使用

NativeMethods.GetRawInputDeviceInfo(hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize);
为我提供设备路径,而不是实际上应该由人类读取的内容。我看到有人提到
BOOLEAN HidD_GetProductString(in]  HANDLE HidDeviceObject,  [out] PVOID  Buffer,  [in]  ULONG  BufferLength);
但每次提到它时,人们都会说它似乎只适用于 HID 设备,不适用于鼠标或键盘。即使我决定使用它,我也不知道如何仅使用原始输入设备结构来获取适当的 BufferLength。

如果有人能够帮助我,我将非常感激。

c# hid raw-input input-devices device-name
1个回答
0
投票

常见的方式是使用WMI,而不是Win32 API调用。这也是系统和监控工具的用途,用于列出和控制本地计算机或公司网络上的任何计算机上的设备。

一般来说谈论“输入设备”是不明确的。毕竟,声音设备是输入设备,而 USB 和蓝牙是设备连接到机器的方式。鼠标显示在

Win32_PointingDevice
类中,键盘显示在
Win32_Keyboard
类中。它们都以
CIM_UserDevice
作为父类

文章使用 PowerShell 和 WMI 查找无线键盘和鼠标展示了如何使用

Get-WMIObject
命令(简称为
gwmi
)轻松列出类中的条目。

跑步

gwmi -class Win32_PointingDevice

列出所有指点设备并显示这些对象源自

CIM_UserDevice

__DERIVATION  : {CIM_PointingDevice, CIM_UserDevice, CIM_LogicalDevice, CIM_LogicalElement...}

查询 CIM_UserDevice 并仅列出类和名称:

gwmi -class CIM_UserDevice |Format-Table CreationClassName,Name,Caption

退货

CreationClassName    Name                                                         Caption
-----------------    ----                                                         -------
Win32_DesktopMonitor Wide viewing angle & High density FlexView Display 3840x2160 Wide viewing angle & High density FlexView Display 3840x2160
Win32_Keyboard       Enhanced (101- or 102-key)                                   Enhanced (101- or 102-key)
Win32_Keyboard       Enhanced (101- or 102-key)                                   Enhanced (101- or 102-key)
Win32_Keyboard       Enhanced (101- or 102-key)                                   Enhanced (101- or 102-key)
Win32_PointingDevice USB Input Device                                             USB Input Device
Win32_PointingDevice HID-compliant mouse                                          HID-compliant mouse
Win32_PointingDevice Synaptics Pointing Device                                    Synaptics Pointing Device
Win32_PointingDevice USB Input Device                                             USB Input Device
Win32_PointingDevice HID-compliant mouse                                          HID-compliant mouse

C# 中的等价物是:

var searcher = new ManagementObjectSearcher("root\\CIMV2",
             "SELECT * FROM CIM_UserDevice");

foreach (ManagementObject queryObj in searcher.Get())
{
    Console.WriteLine("Class: {0}\tName {1}", queryObj["CreationClassName"],queryObj["Name"]);
}
© www.soinside.com 2019 - 2024. All rights reserved.