The Great ReLLeRtR
The Great ReLLeRtR

Reputation: 15

How do I get a raw input device's proper display name in C#?

I am making a simple program to enumerate all of the devices currently connected to your Windows along with their pointers, then display the proper name for the device you clicked a button on your WPF window in C#.

I have finished writing the code to collect all the devices input using

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));
}

and now all I need is to just get the product name for the device, aka something like TUF GAMING M5 in case of my mouse, which is how it shows up in my Device Manager window.

It appears as though using NativeMethods.GetRawInputDeviceInfo(hDevice, RawInputDeviceInfo.RIDI_DEVICENAME, IntPtr.Zero, ref pcbSize); gets me the device path and not something actually meant to be read by a human. I've seen people mention BOOLEAN HidD_GetProductString(in] HANDLE HidDeviceObject, [out] PVOID Buffer, [in] ULONG BufferLength); but everytime it's mentioned, people say it only seems to work for HID devices and not mouses or keyboards. Even if I did decide to use it, I don't know how to get the appropriate BufferLength for it with nothing but a raw input device struct.

If anyone is able to help me with this, I would strong appreciate it.

Upvotes: 0

Views: 283

Answers (1)

Panagiotis Kanavos
Panagiotis Kanavos

Reputation: 131706

The common way is to use WMI, not Win32 API calls. That's what system and monitoring tools as well, to list and control devices on the local machine or any machine on a company's network.

You can query the Win32_KeyboardDevice and Win32_PointingDevice classes to find keyboards and mice or other pointing devices, or query their parent CIM_UserDevice

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

foreach (var queryObj in searcher.Get())
{
    Console.WriteLine("Class: {0}\tName {1}", 
                      queryObj["CreationClassName"],
                      queryObj["Name"]);
}

Talking about "input devices" in general is ambiguous. After all, sound devices are input devices while USB and Bluetooth is how devices connect to the machine. Mice are shown in the Win32_PointingDevice class, keyboards in the Win32_Keyboard class. Both of them have CIM_UserDevice as a parent class

The article Use PowerShell and WMI to Find Wireless Keyboard & Mouse shows how to easily list entries in a class using the Get-WMIObject command, or gwmi for short.

Running

gwmi -class Win32_PointingDevice

Lists all pointing devices and shows that the objects are derived from CIM_UserDevice among others

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

Querying CIM_UserDevice and listing just the class and name with :

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

Returns

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

The equivalent in C# is :

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"]);
}

Upvotes: 0

Related Questions