GetRawInputDeviceInfo (user32)
Last changed: kosia4enko.g@gmail.com-94.45.79.85

.
Summary
The GetRawInputDeviceInfo function gets information about the raw input device.

C# Signature:

[DllImport("user32.dll", SetLastError=true)]
static extern uint GetRawInputDeviceInfo(IntPtr hDevice, uint uiCommand, IntPtr pData, ref uint pcbSize);

C# User-Defined Types:

    [StructLayout(LayoutKind.Sequential)]
    internal struct RID_DEVICE_INFO_HID
    {
        [MarshalAs(UnmanagedType.U4)]
        public int dwVendorId;
        [MarshalAs(UnmanagedType.U4)]
        public int dwProductId;
        [MarshalAs(UnmanagedType.U4)]
        public int dwVersionNumber;
        [MarshalAs(UnmanagedType.U2)]
        public ushort usUsagePage;
        [MarshalAs(UnmanagedType.U2)]
        public ushort usUsage;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct RID_DEVICE_INFO_KEYBOARD
    {
        [MarshalAs(UnmanagedType.U4)]
        public int dwType;
        [MarshalAs(UnmanagedType.U4)]
        public int dwSubType;
        [MarshalAs(UnmanagedType.U4)]
        public int dwKeyboardMode;
        [MarshalAs(UnmanagedType.U4)]
        public int dwNumberOfFunctionKeys;
        [MarshalAs(UnmanagedType.U4)]
        public int dwNumberOfIndicators;
        [MarshalAs(UnmanagedType.U4)]
        public int dwNumberOfKeysTotal;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct RID_DEVICE_INFO_MOUSE
    {
        [MarshalAs(UnmanagedType.U4)]
        public int dwId;
        [MarshalAs(UnmanagedType.U4)]
        public int dwNumberOfButtons;
        [MarshalAs(UnmanagedType.U4)]
        public int dwSampleRate;
        [MarshalAs(UnmanagedType.U4)]
        public int fHasHorizontalWheel;
    }

    [StructLayout(LayoutKind.Explicit)]
    internal struct RID_DEVICE_INFO
    {
        [FieldOffset(0)]
        public int cbSize;
        [FieldOffset(4)]
        public int dwType;
        [FieldOffset(8)]
        public RID_DEVICE_INFO_MOUSE mouse;
        [FieldOffset(8)]
        public RID_DEVICE_INFO_KEYBOARD keyboard;
        [FieldOffset(8)]
        public RID_DEVICE_INFO_HID hid;
    }
    internal enum DeviceInfoTypes {
        RIDI_PREPARSEDDATA = 0x20000005,
        RIDI_DEVICENAME = 0x20000007,
        RIDI_DEVICEINFO = 0x2000000B
    }

VB Signature:

Declare Function GetRawInputDeviceInfo Lib "user32.dll" Alias "GetRawInputDeviceInfoW" (ByVal hDevice As IntPtr, ByVal uiCommand As DeviceInfoTypes, ByVal pData As IntPtr, ByRef pcbSize As UInteger) As Integer

VB User-Defined Types:

Public Enum DeviceInfoTypes As UInteger
     RIDI_PREPARSEDDATA = &H20000005
     RIDI_DEVICENAME = &H20000007
     RIDI_DEVICEINFO = &H2000000B
End Enum

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    ' Enumerating devices with a known VID
    ' First half heavily borrowed from C# example on GetRawInputDeviceList()
    Sub GetAllRawDeviceVID()

         Dim deviceCount As UInt32 = 0
         Dim dwSize As Integer = CInt(Marshal.SizeOf(GetType(RAWINPUTDEVICELIST)))
         Dim retValue As UInt32 = GetRawInputDeviceList(IntPtr.Zero, deviceCount, dwSize)
         If retValue <> 0 Then Exit Sub       ' handle this

         Dim devPtr As IntPtr = Marshal.AllocHGlobal(CType(dwSize * deviceCount, Integer))
         retValue = GetRawInputDeviceList(devPtr, deviceCount, dwSize)

         Dim deviceList As RAWINPUTDEVICELIST
         Dim rawDevs As New Dictionary(Of IntPtr, RawInputDeviceType)

         For i As Integer = 0 To (deviceCount - 1)
         deviceList = CType(Marshal.PtrToStructure(New IntPtr((devPtr.ToInt32() + (dwSize * i))), GetType(RAWINPUTDEVICELIST)), RAWINPUTDEVICELIST)
         If deviceList.dwType = RawInputDeviceType.KEYBOARD Then
             rawDevs.Add(deviceList.hDevice, deviceList.dwType)
         End If

         Next

         Dim pcbSize As UInt32 = 0
         Dim deviceName As String = String.Empty
         Dim lstHDevices As New List(Of String)

         For Each itm As KeyValuePair(Of IntPtr, RawInputDeviceType) In rawDevs
         GetRawInputDeviceInfo(itm.Key, DeviceInfoTypes.RIDI_DEVICENAME, IntPtr.Zero, pcbSize)
         Dim pData As IntPtr = Marshal.AllocHGlobal(CType(pcbSize, Integer))

         GetRawInputDeviceInfo(itm.Key, DeviceInfoTypes.RIDI_DEVICENAME, pData, pcbSize)
         deviceName = Marshal.PtrToStringAuto(pData)

         If deviceName.Contains("VID_05FE") Then lstHDevices.Add(deviceName)

         Next

         ' do something

     End Sub

Documentation